请考虑以下事项:
$PDOStatement = $PDO->prepare($query);
是否可以从$PDOStatement
实例获取$PDO
实例?
目前,这是不可能的。即使PDOStatement的每个实例都存储了一个用于创建它的数据库句柄(引用PHP 5.6的lxr):
/* 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_t
的引用(链接):
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;