English 中文(简体)
PHP - 多种不同的附属数据库
原标题:PHP - multiple different databases dependency injected class

我过去几小时试图找到“最佳”的答案,最符合逻辑的答案是,撰写一个网站数据库,同时连接一个信箱和一台我的仪。 此外,我还要采用一种依赖性注射设计,但对整个概念是新的。

到目前为止,我已经来到了......。

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();
        }
    }

}

显然,重复成文法违反了南盟的做法。 我知道并看到了许多多条干 connection连接的例子,但大多数是同样的驱动力,没有拖网提供发展能力。

我还要补充指出,我已考虑将连接细节列入数据库类别构造中......。

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

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

但我不知道这是否真的是一个好主意,也不知道它会如何与移民部合作。

感谢!

问题回答

你们首先应为所有非行业务建立接口。

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

Then have different driver classes implementing this interface

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

这样,你就能够轻易使用依赖注射。

class Test
{
   private $db;

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

你可以称之为:

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

To avoid duplicated code you can 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();

或者使用成分,让Db类人使用不同的驱动力,采用分辨别的方法

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();

在php5.4中,将存在海峡,因此将有更多的办法避免代码重复。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签