是否存在不会调用此方法的情况?
我正在考虑在缓存对象被销毁之前将一个重要变量存储到持久缓存中。这个变量在页面中多次使用,所以每次变量改变时我都不想用它更新缓存......
答案 0 :(得分:14)
我们有一个班级:
class A {
public function __construct(){
echo "Construct\n";
}
public function __destruct(){
echo "Destruct\n";
}
}
测试代码:
$test = new A();
die( "Dead\n"); // Will output Construct; dead; Destruct
$test = new A();
throw new Exception("Blah\n"); // Construct, Fatal error (no destruct)
$test = new A();
require_once( 'invalid_file.php'); // Construct, Fatal error (no destruct)
所以基本上:在没有调用析构函数时会出现(致命错误)。
啊,这个问题和这个问题答案相同:When will __destruct not be called in PHP?(+/-)
答案 1 :(得分:3)
只要没有对该特定对象的引用,或者在关闭序列期间,就会调用它。该手册还说明当使用exit()终止脚本时调用析构函数。
除了TimWolla指出的问题,我不知道PHP析构函数有任何问题。
答案 2 :(得分:1)
使用Windows似乎至少 是一个问题:https://github.com/WoltLab/WCF/blob/ff7e6ed381f2ccab7f51220f97087921133b2237/wcfsetup/install/files/lib/system/WCF.class.php#L122
我不知道这是否仍然相关。