【发布时间】:2012-08-06 14:26:00
【问题描述】:
我一直想知道 OOP 连接的效率。示例结构如下所示。每个类由文件夹和类名分隔。例如:
/mysql/mysql.class.php //holds general connect and disconnect functions
/mysql/query.class.php //holds all the queries made to the db (uses basic mysql ^ )
/library/application.class.php //holds all the algorithms (uses queries to db ^ )
在执行算法时,application.class.php 调用 query.class.php(当它需要查询时),后者调用 mysql.class.php 进行连接。
这两种方法中哪一种是正确的(如果有的话)?有没有更简单、更有效的方法?
这是做两件事的相同方法(哪个更有效??):
require_once('/mysql/mysql.class.php ');
require_once('/mysql/query.class.php ');
$mysqlClass = new MYSQL();
$queryClass = new Query();
class Application {
public function getVar() {
$param = foo;
$db = $mysqlClass->connect(); //connection to mysql
$queryClass->callsomequerymethod($db, $param); //pass the db connection to function
}
}
VS
class Application {
private $db;
public function __construct(DatabaseConnection $db, QueryClass $queryclass){
$this->db = $db;
}
public function getVar() {
$param = foo;
$db = $this->db;
$queryclass->callsomequerymethod($db, $param);
}
}
【问题讨论】:
标签: php mysql performance oop constructor