問題描述
我對 OOP 風格的 PHP 很陌生,我也在嘗試實現 PDO.我在網上找到了這個處理數據庫連接的不錯的小類,但是我不知道如何從另一個類訪問它.代碼如下:
I am very new to OOP styled PHP, and I am trying to implement PDO as well. I found this nice little class online which handles the database connection, however I have no idea how to access it from another class. Here is the code:
class PDO_DBConnect {
static $db ;
private $dbh ;
private function PDO_DBConnect () {
$db_type = 'mysql'; //ex) mysql, postgresql, oracle
$db_name = 'postGal';
$user = 'user' ;
$password = 'pass' ;
$host = 'localhost' ;
try {
$dsn = "$db_type:host=$host;dbname=$db_name";
$this->dbh = new PDO ( $dsn, $user, $password);
$this->dbh->setAttribute(PDO::ATTR_PERSISTENT, true);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch ( PDOException $e ) {
print "Error!: " . $e->getMessage () . "
" ;
die () ;
}
}
public static function getInstance ( ) {
if (! isset ( PDO_DBConnect::$db )) {
PDO_DBConnect::$db = new PDO_DBConnect ( ) ;
}
return PDO_DBConnect::$db->dbh;
}
}
$db_handle = PDO_DBConnect::getInstance();
class Person
{
function __construct()
{
$STMT = $db_handle->prepare("SELECT title FROM posts WHERE id = ? AND author = ? LIMIT 20");
$STMT->execute(array('24', 'Michael'));
while ($result = $STMT->fetchObject())
{
echo $result->title;
echo "<br />";
}
}
}
如何訪問我的 Person 類中的 $db_handle
變量?我是否必須在 Person 類中實例化變量?如果是這樣,那是否意味著我將始終必須將其稱為 $this->db_handle
?我希望避免這種情況.(我對類的變量作用域仍然只有非常基本的了解)
How can I gain access to the $db_handle
variable inside of my Person class? Do i have to instantiate the variable inside of the Person class? If so, does that mean I will always have to call it as $this->db_handle
? I was hoping to avoid that. (I still only have a very basic understanding of variable scope with classes)
推薦答案
有(至少)三種方法來處理這個問題.最便攜且經常被推薦的方法稱為依賴注入",您可以通過它的 __construct()
將數據庫句柄傳遞到您的類中,并將其存儲在類變量中.需要使用 $this->db
訪問它,就像您不想做的那樣.
There are (at least) three ways to handle this. The most portable, and oft recommended is called "dependency injection", whereby you pass the database handle into your class via its __construct()
and store it in a class variable. Requires accessing it with $this->db
like you didn't want to have to do.
class Person {
// Member to hold the db handle
public $db;
public function __construct($db_handle) {
// Assign the handle to a class member variable in the constructor
$this->db = $db_handle;
}
public function otherFunc() {
$this->db; // do something
}
}
$person = new Person($db_handle);
下一個方法是在構造函數中實例化 $db_handle
而不是將其傳入.這有點難以測試和調試.
Next method would be to instantiate the $db_handle
inside the constructor rather than passing it in. This is a little harder to test and debug.
class Person {
public $db;
public function __construct() {
$this->db = PDO_DBConnect::getInstance();
}
}
最后,您可以在課堂中使用 $db_handle
作為 global
調用它.這可能是最難閱讀和調試的.
Finally, you can call $db_handle
as a global
whenever you use it in your class. This is perhaps the hardest to read and debug.
class Person {
public function __construct() {
global $db_handle;
}
public function otherFunc() {
global $db_handle;
$db_handle; // do something
}
}
這篇關于PHP 和 PDO 類問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!