2014-12-01 86 views
1
獲取PDO

考慮以下幾點:從PDOStatement對象

$PDOStatement = $PDO->prepare($query); 

是否有可能從$PDOStatement實例得到$PDO實例?

+0

'的var_dump($ PDOStatement對象)'會顯示語句對象中的任何反向鏈接。 – 2014-12-01 21:23:09

+3

['PDOStatement'](http://php.net/manual/en/class.pdostatement.php)的文檔似乎沒有顯示任何方式來做到這一點。我可以問*你爲什麼要這樣做?你想在這裏解決的*實際*問題是什麼? – 2014-12-01 21:35:43

+1

這是微不足道的。我正在編寫的類需要一個「PDOStatement」實例和生成它的「PDO」實例。我想知道是否可以簡化課程的界面。 – 2014-12-01 21:46:11

回答

1

目前,這是不可能的。儘管PDOStatement對象的每個實例存儲用於創建它(quoting lxr for PHP 5.6)一DB手柄:

/* represents a prepared statement */ 
543 struct _pdo_stmt_t { 
544 /* these items must appear in this order at the beginning of the 
545  struct so that this can be cast as a zend_object. we need this 
546  to allow the extending class to escape all the custom handlers 
547  that PDO declares. 
548 */ 
549 zend_object std; 
550 
... 
572 /* we want to keep the dbh alive while we live, so we own a reference */ 
573 zval database_object_handle; 
574 pdo_dbh_t *dbh; 

...它不是通過公共方法暴露。


這可能是值得一記,反過來pdo_dbh_t實例可以(至少看起來如此)引用存儲到pdo_stmt_tlink)的:

427 /* represents a connection to a database */ 
428 struct _pdo_dbh_t { 
... 
501 /* when calling PDO::query(), we need to keep the error 
502  * context from the statement around until we next clear it. 
503  * This will allow us to report the correct error message 
504  * when PDO::query() fails */ 
505 pdo_stmt_t *query_stmt;