tags:

views:

69

answers:

2

Is it possible to extend PHP PDO statement class to add custom methods to it? This would be different from extending the base PDO class. If so, how would one go about doing it since the statement class is only returned when running queries through the PDO class?

A: 

Yes, it's possible. You can call parent methods inside your child class by prepending the method name with parent::, e.g.:

class MyPDO extends PDO {
  public function query($sql) {
    $result = parent::query($sql);
    return $result;
  }
}
Emil Vikström
@Emil, I'm trying to specifically extend the Statement class, not the PDO class. http://www.php.net/manual/en/class.pdostatement.php
Levi Hackwith
+4  A: 

You can set the class with PDO::setAttribute():

PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).

Example:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Custom', array($pdo)));
Gordon