我一直想知道 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);
}
}