How does PDO::prepare() generates a PDOStatement object?

116 views Asked by At

How is the readonly $queryString property being set inside PDOStatement class by the PDO::prepare() execution?

Given the class definition, I don't see any functions which would set that query. Does that mean PDOStatement class can't be used if it's not generated by the PDO class instance through PDO::prepare() function?

2

There are 2 answers

0
Your Common Sense On BEST ANSWER

PDOstatement is created internally, by means of C code. And of course C code can set any properties directly.

And yes, you cannot use PDOStatement class (for anything useful) if it's not generated by the PDO class instance through PDO::prepare() (or query()).

However, you can redeclare and tell PDO to use it instead of standard one using code like this

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['myPDOStatement', [$pdo]]);
1
AbcAeffchen On

From the manual:

Introduction


[The PDOStatement class] Represents a prepared statement and, after the statement is executed, an associated result set.

This looks like it has to be created by PDO::prepare().

But is this a problem? I cannot say that I came across a situation where I want to create an PDOStatement object by hand.