Search code examples
phpmysqlpreparepdo

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


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?


Solution

  • 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]]);