I am writing a PDO extension. I would like prepare method of PDO to not call upon its parent and instead construct the PDOStatement of its own.
class Foo extends \PDO {
public function prepare ($statement, $driver_options = []) {
// return parent::prepare($statement, $driver_options);
// What does parent::prepare do?
}
}
It appears that PDO prepare method simply constructs PDOStatement instance, but then PDOStatement constructor does not have all the parameters that I would expect there (the statement itself, etc).
How to write PDO prepare method in user-land?
Note that I am aware about the presence of the PDO::ATTR_STATEMENT_CLASS and that it can be used to name the PDOStatement class to construct. I am interested whether that PDOStatement can be constructed in the user-land.
You can't instantiate a PDOStatement directly. It's hard-coded to disallow it.
I find this in the PHP 5.5 source, in ext/pdo/pdo_stmt.c:
static PHP_FUNCTION(dbstmt_constructor) /* {{{ */
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "You should not create a PDOStatement manually");
}
/* }}} */
You should create a PDOStatement by calling PDO::query() or PDO::prepare().
It's not clear from your question why you would need to instantiate a PDOStatement directly. I think you need to rethink your goals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With