开发者

How can I wrap the PHP PDO class into my own class?

开发者 https://www.devze.com 2022-12-16 12:40 出处:网络
Sorry if this is wrong or confusing I am very new to using classes and I would like to start learning more about using PDO with mysql.

Sorry if this is wrong or confusing I am very new to using classes and I would like to start learning more about using PDO with mysql.

Here is an example code from php.net

<?php
/* Execute a prepared statement by passing an array of values */
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories开发者_如何学编程 < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchAll();
?>

Generally with just the regular mysql extension, I like to wrap all the default methods into my own method, for example when I dont use PDO I have a Class

database class

when I call this class like this...

$db = new Database;
$db->execute('SQL QUERY GOES HERE');

Inside my execute method I would have it run a regualr query but also check if I am admin, If I am admin and running the query, then I count the queries on a page to a session and show it.

SO using the PDO class, how could I wrap all it's methods into my own class so I could do something like count the queries ran on a page for certain users?


Extending PDO and PDOStatement has already been mentioned, here's an (*cough* undocumented) example:

class MyPDOStatement extends PDOStatement {
  public $logExecution = false;
  protected $listeners = array();

  public function execute($input_parameters=array()) {
    foreach( $this->listeners as $cb ) {
      call_user_func($cb);
    }
    return parent::execute($input_parameters);
  }

  public function addListener($cb) {
    $this->listeners[] = $cb;
  }
}

class MyPDO extends PDO {
  const ATTR_LOG_QUERIES = 'MyPDO_ATTR_LOG_QUERIES';

  public function prepare($statement, $driver_options=array()) {
    echo "MyPDO::prepare()\n";
    // tell PDO::prepare to create an instance of MyPDOStatement as the statement object
    $driver_options[PDO::ATTR_STATEMENT_CLASS] = array('MyPDOStatement');
    $stmt = parent::prepare($statement, $driver_options); 

    if ( isset($driver_options[MyPDO::ATTR_LOG_QUERIES]) ) {
      $stmt->addListener($driver_options[MyPDO::ATTR_LOG_QUERIES]);
    }
    return $stmt;
  }
}

class Foo {
  public $counter = 0;
  public function onBar() {
    echo "Foo::onBar()\n";
    $this->counter += 1;
  }
}

$pdo = new MyPDO('sqlite::memory:'); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('CREATE TABLE foo ( x INTEGER, y INTEGER)');

$foo = new Foo;
$stmt = $pdo->prepare(
  'INSERT INTO foo (x,y) VALUES (?,?)',
  array(MyPDO::ATTR_LOG_QUERIES=>array($foo, 'onBar'))
);
echo 'class($stmt)=', get_class($stmt), "\n";
$stmt->execute(array(1,1));
$stmt->execute(array(2,2));

echo 'counter=', $foo->counter;

prints

MyPDO::prepare()
class($stmt)=MyPDOStatement
Foo::onBar()
Foo::onBar()
counter=2    


You can just extend the built-in PDO class.

<?php
class Database extends PDO {
     // your methods and properties here
}


Why not just use a PDO CRUD class?


Not exactly related, but I've managed to wrap the most common functions of PDO inside a function however, you should probably follow Mike advice. =)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号