2015-10-27 66 views
-1

我有一個連接到數據庫的PDO對象,我有5個類需要數據庫連接的方法。對於我正在構建$db的每個班級。這是真的嗎?如果不是,我該怎麼辦?

try { 
    $config['db'] = array(
     'host'  => 'localhost', 
     'username' => 'xxxxxx', 
     'password' => 'xxxxxx', 
     'dbname' => 'table_name' 
    ); 
    $db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 
} catch (Exception $e) { 
    echo "!"; 
} 

//classA 
class ClassA{ 
    private $db; 
    public function __construct(PDO $db){ 
     $this->db = $db; 
    } 

    public function methodA1($someId){ 
     $res = $this->db->query("SELECT * FROM bla WHERE id = $someId "); 
     return $res->fetchAll(PDO::FETCH_ASSOC); 
    } 
} 

//classB 
class ClassB{ 
    private $db; 
    public function __construct(PDO $db){ 
     $this->db = $db; 
    } 

    public function methodB1($someId){ 
     $res = $this->db->query("SELECT * FROM bla WHERE id = $someId "); 
     return $res->fetchAll(PDO::FETCH_ASSOC); 
    } 
} 

然後我箱子這些類的新對象,如

$classAObject = new ClassA($db); 
$classBObject = new ClassB($db); 

雖然我創造我的目標,我是否連接到DB 2倍?

+0

你做了多少次'新的PDO(...)'這是你連接數據庫的次數。 – RiggsFolly

回答

0

如果你傳遞每一個對象在同一$db對象的引用,你正在做的是初始化對象的屬性,PDO $db等於傳遞的PDO對象,所以,不,我不相信你將不止一次連接到數據庫。

1

它連接一次。

您正在使用相同的PDO對象,因此它只使用您在包含文件時初始化的對象。

我建議你讓它成爲一個單身人士,所以當你使用PDO對象時,你總是會得到已被初始化的對象,這將被所有的連接使用。

Model類

class Model { 
    private $_mysql; 

    public function __construct() { 
     //Get "singleton" instance of the DatabaseConnector (shared between all Models) 
     $this->_mysql = DatabaseConnector::getInstance(); 
    } 
} 

DatabaseConnector類

class DatabaseConnector extends Singleton { 
    private $_mysql; 

    public function __construct() { 
     $this->_mysql = new PDO(...); 
    } 

    public function beginTransaction() { 
     return $this->_mysql->beginTransaction(); 
    } 

    public function commit() { 
     return $this->_mysql->commit(); 
    } 

    public function rollback() { 
     return $this->_mysql->rollback(); 
    } 
} 

Singleton類

class Singleton 
{ 
/** 
* @var Singleton The reference to *Singleton* instance of this class 
*/ 
private static $instance; 

/** 
* Returns the *Singleton* instance of this class. 
* 
* @return Singleton The *Singleton* instance. 
*/ 
public static function getInstance() 
{ 
    if (null === static::$instance) { 
     static::$instance = new static(); 
    } 

    return static::$instance; 
} 

/** 
* Protected constructor to prevent creating a new instance of the 
* *Singleton* via the `new` operator from outside of this class. 
*/ 
protected function __construct(){} 

/** 
* Private clone method to prevent cloning of the instance of the 
* *Singleton* instance. 
* 
* @return void 
*/ 
private function __clone(){} 

/** 
* Private unserialize method to prevent unserializing of the *Singleton* 
* instance. 
* 
* @return void 
*/ 
private function __wakeup(){} 

} 

您可以檢查此爲更詳細singleton

貝斯ides,你最好分開你的文件在一個文件中只有一個類。

+0

您能否提供一個單例方法? – hakiko

+0

@hakkikonu我更新我的答案 – jayxhj