2014-12-01 84 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;