Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct PDOStatement class?

Tags:

php

pdo

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.

like image 882
Gajus Avatar asked Aug 29 '26 19:08

Gajus


1 Answers

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.

like image 182
Bill Karwin Avatar answered Sep 04 '26 16:09

Bill Karwin