【问题标题】:How can I wrap the PHP PDO class into my own class?如何将 PHP PDO 类包装到我自己的类中?
【发布时间】:2023-03-09 04:25:01
【问题描述】:

对不起,如果这是错误的或令人困惑的,我对使用类很陌生,我想开始学习更多关于在 mysql 中使用 PDO。

这是来自 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();
?>

通常只使用常规的 mysql 扩展,我喜欢将所有默认方法包装到我自己的方法中,例如当我不使用 PDO 时我有一个类

数据库类

当我这样称呼这个类时......

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

在我的执行方法中,我会让它运行一个常规查询,但还要检查我是否是管理员,如果我是管理员并运行查询,那么我会将页面上的查询计数到会话并显示它。

那么使用 PDO 类,我如何将它的所有方法包装到我自己的类中,这样我就可以做一些事情,比如计算某些用户在页面上运行的查询?

【问题讨论】:

  • 听起来像是模型中的逻辑。把它们分开怎么样?提示:MVC。

标签: php mysql pdo


【解决方案1】:

扩展 PDO 和 PDOStatement 已经提到过,这里有一个(*cough* undocumented)示例:

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;

打印

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

【讨论】:

    【解决方案2】:

    您可以只扩展内置的 PDO 类。

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

    【讨论】:

      【解决方案3】:

      为什么不直接使用PDO CRUD 类?

      【讨论】:

        【解决方案4】:

        不完全相关,但我已经设法wrap the most common functions of PDO inside a function 但是,您可能应该follow Mike advice。 =)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-01
          • 2014-04-02
          • 1970-01-01
          • 1970-01-01
          • 2018-06-25
          • 1970-01-01
          • 1970-01-01
          • 2010-11-23
          相关资源
          最近更新 更多