【发布时间】:2014-02-10 11:38:24
【问题描述】:
我已经从 mysqli 类扩展了我的数据库模型,但我不希望每次实例化一个新模型时它都重新连接,因为它确实减慢了整个脚本的速度。
我有一个从mysqli 扩展的Database 类,以及从Database 扩展的其他模型类。
我的Database 班级看起来像这样:
class Database extends mysqli {
// some properties ...
public function __construct() {
parent::__construct(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DATABASE);
$this->set_charset(MySQL_CHARACTER_SET);
// initializing stuff ..
}
// some other methods...
}
示例模型类如下所示:
class User extends Database {
protected $table = "users";
public function __construct($id = null) {
// Calling the parent constructor.
parent::__construct();
if($id) {
// This is the current logged-in user.
// Importing all session data into this object.
$this->data->import($this->session->all());
// ^^ This imports all the user related session data into this object
// i don't think it's relevant but i'll leave it here just in case.
}
}
我的问题是,
如何检查是否有活动的
mysqli连接?如何防止它重新连接并改用活动连接?
我还可以采用哪些其他方法?
我应该迁移到
PDO,我必须这样做吗? 为什么?
P.S.:迁移到 PDO 将需要大量返工,因为我已经在 mysqli 上构建了一些东西。
【问题讨论】: