I have spent the final several hrs looking for an response to the "best", most logical, etc method to write a php database class to concurrently connect with one postgresql db and something mysql db. Also, Let me adopt an addiction Injection design but am a new comer to that whole concept.

To date I have develop...

class Database {

    public function PgSqlConnect() {
            /* Connect to database */
        $host = 'localhost';
        $dbname = '---';
        $user = '---';
        $pass = '---';
        $timeout = 5;   /* seconds */

        try {
            $pgsql_dbh = new PDO("pgsql:host=$host; dbname=$dbname", $user, $pass); 
            $pgsql_dbh->setAttribute( PDO::ATTR_TIMEOUT, $timeout ); 
            $pgsql_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

            return $pgsql_dbh;
        } catch( PDOException $e ) {
            echo 'Unable to connect to database: ' . $e->getMessage();
        }
    }


    public function MySqlConnect() {
            /* Connect to database */
        $host = 'localhost';
        $dbname = '---';
        $user = '---';
        $pass = '---';
        $timeout = 5;   /* seconds */

        try {
            $mysql_dbh = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass); 
            $mysql_dbh->setAttribute( PDO::ATTR_TIMEOUT, $timeout ); 
            $mysql_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

            return $mysql_dbh;
        } catch( PDOException $e ) {
            echo 'Unable to connect to database: ' . $e->getMessage();
        }
    }

}

Clearly the copied code violates the DRY approach. I understand and also have seen many good examples of multiple db connections, but many cope with same driver and do not provide DI capacity.

I ought to include that I have considered placing the bond particulars in to the Database class constructor as...

$driver = 'mysql';
...
$mysqldb = new Database($driver,$un,$pw,...);

$driver = 'pgsql';
...
$pgsqldb = new Database($driver,$un,$pw,...);

but I'm not sure if that's really advisable nor how good it might use DI.

Thank you!

You need to create an interface first for the DB procedures.

interface IDatabase
{
    function connect();
    function query();
    ...
}

Then have different driver classes applying this interface

class MySQLDB implements IDatabase
{
}
class PGSQLDB implements IDatabase
{
}

By doing this it is simple to use dependency injection.

class Test
{
   private $db;

   function __construct(IDatabase $db)
   {
        $this->db = $db;
   }
}

You are able to refer to it as as:

$mysqldb = new MySQLDB();
$test = new Test($mysqldb);
or
$pgsqldb = new PGSQLDB();
$test = new Test($pgsqldb);

To prevent copied code you are able to extend an abstract class

abstract class AbstractDb {
    public function connect() {
        // common code to avoid duplication
        echo 'connected!';
    }

    abstract public function escapeField();
    abstract public function escapeValue();
}

class MySQL extends AbstractDb {
    public function escapeField() {
        // Db-specific method
    }

    public function escapeValue() {
        // Db-specific method
    }
}

$db = new MySQL;
$db->connect();

Or use composition, and allow the Db class make use of a different driver for db-specific techniques

interface IDriver {
    public function escapeField();
    public function escapeValue();
}

class MySQLDriver implements IDriver {
    public function escapeField() {
        // Db-specific method
    }

    public function escapeValue() {
        // Db-specific method
    }
}

class Db {
    public function __construct($driver) {
        $this->driver = $driver;
    }

    public function connect() {
        // common code here ? idk, it's just an example
        echo 'connect!';
    }

    // this method is db-specific, so we call the driver
    public function escapeField($field) {
        return $this->driver->escapeField($field);
    }

    public function escapeValue() {
        // same here
    }
}

$db = new Db(new MySQLDriver);
$db->connect();

In php 5.4 you will see traits, so you will see more methods to avoid code duplication.