【发布时间】:2012-08-02 17:20:38
【问题描述】:
我有一个包含许多类的 PHP 项目,以及我从网上下载的一个 PDO Wrapper 类,看起来不错:
http://code.google.com/p/php-pdo-wrapper-class/
我的课程现在是这样的:
class product {
__constructor($id = 0) {
//mysql_query and store the product row in $this->row;
}
edit($array) {
//another obvious mysql_query...
}
所以它们非常简单,我想将所有 mysql_query 移动到 PDO 以确保安全。我有变量:
$db = new pdo('localhost', 'user', 'pass');
在我的包含文件的第一行中,在类之前正确定义了工作。但是,我不想写:
global $db;
在每个函数的开头。我考虑将 __construct() 函数更改为更像:
__construct($id = 0, $db = null) {
$this->db = $db;
}
然后从那里引用,但问题是我必须更改整个网站中的每个构造函数。有没有办法解决这个问题,使我能够对现有代码进行最少的编辑?
【问题讨论】: