静态实例变量的析构函数方法?
Destructor method for static instance variable?
这是我的 class:
class MySQLDB extends PDO{
private $_connection;
private static $_instance; //The single instance
private $_host = MYSQL_SERVER_NAME;
private $_username = MYSQL_SERVER_USERNAME;
private $_password = MYSQL_SERVER_PASSWORD;
private $_database = MYSQL_SERVER_DBNAME;
/*
Get an instance of the MySQLDB
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Get PDO MySQL connection
public function getConnection() {
return $this->_connection;
}
public function __construct(){
try {
$this->_connection = new PDO("mysql:host={$this->_host};dbname={$this->_database};charset=utf8", $this->_username, $this->_password);
$this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function __destruct(){
// unset($this->_connection);
self::$_instance = null;
}
}
这是我的主要内容:
$db = MySQLDB::getInstance();
$db->__destruct();
print_r($db).'<br /><br />';
这是输出:
MySQLDB Object ( [_host:MySQLDB:private] => localhost [_username:MySQLDB:private] => username [_password:MySQLDB:private] => [_database:MySQLDB:private] => dbname )
我的问题:为什么我会得到这个输出?如果我像上面那样调用 __destruct() 方法,我不应该只得到一个空输出——因为我正在使实例无效吗?
或者也许我应该在 main 中使用它:
$db = null;
print_r($db).'<br /><br />';
如何确保已关闭连接并终止对象?
不,$db->__destruct()
只是调用 __destruct()
方法。它不会神奇地取消设置变量。
析构函数以相反的方式工作:当对象在没有变量引用它之后被垃圾收集器销毁时,然后自动调用析构函数。
您想做的是:
unset($db);
但请注意,不能保证在那个时刻调用析构函数。首先,该对象不能分配给任何其他变量,其次,清理未引用对象的垃圾收集器是定期执行的,而不是在每个命令之后执行。
在您的例子中,对象仍然被 MySQLDB::$_instance
引用。如果将取消设置 self::$_instance
的当前 __destruct
方法更改为静态方法 resetInstance()
,则可以使用:
MySQLDB::resetInstance();
unset($db);
然后两个引用都消失了,最终将调用 descstructor(同样,如果对象上没有其他引用)。
有关引用计数主题的更多信息,请参阅垃圾收集器手册:http://php.net/manual/de/features.gc.refcounting-basics.php
这是我的 class:
class MySQLDB extends PDO{
private $_connection;
private static $_instance; //The single instance
private $_host = MYSQL_SERVER_NAME;
private $_username = MYSQL_SERVER_USERNAME;
private $_password = MYSQL_SERVER_PASSWORD;
private $_database = MYSQL_SERVER_DBNAME;
/*
Get an instance of the MySQLDB
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Get PDO MySQL connection
public function getConnection() {
return $this->_connection;
}
public function __construct(){
try {
$this->_connection = new PDO("mysql:host={$this->_host};dbname={$this->_database};charset=utf8", $this->_username, $this->_password);
$this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function __destruct(){
// unset($this->_connection);
self::$_instance = null;
}
}
这是我的主要内容:
$db = MySQLDB::getInstance();
$db->__destruct();
print_r($db).'<br /><br />';
这是输出:
MySQLDB Object ( [_host:MySQLDB:private] => localhost [_username:MySQLDB:private] => username [_password:MySQLDB:private] => [_database:MySQLDB:private] => dbname )
我的问题:为什么我会得到这个输出?如果我像上面那样调用 __destruct() 方法,我不应该只得到一个空输出——因为我正在使实例无效吗?
或者也许我应该在 main 中使用它:
$db = null;
print_r($db).'<br /><br />';
如何确保已关闭连接并终止对象?
不,$db->__destruct()
只是调用 __destruct()
方法。它不会神奇地取消设置变量。
析构函数以相反的方式工作:当对象在没有变量引用它之后被垃圾收集器销毁时,然后自动调用析构函数。
您想做的是:
unset($db);
但请注意,不能保证在那个时刻调用析构函数。首先,该对象不能分配给任何其他变量,其次,清理未引用对象的垃圾收集器是定期执行的,而不是在每个命令之后执行。
在您的例子中,对象仍然被 MySQLDB::$_instance
引用。如果将取消设置 self::$_instance
的当前 __destruct
方法更改为静态方法 resetInstance()
,则可以使用:
MySQLDB::resetInstance();
unset($db);
然后两个引用都消失了,最终将调用 descstructor(同样,如果对象上没有其他引用)。
有关引用计数主题的更多信息,请参阅垃圾收集器手册:http://php.net/manual/de/features.gc.refcounting-basics.php