class MyDestructableClass {
function __construct() {
print "\nIn constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "\nDestroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
当上述脚本处于复杂的环境中时,
exit
时不会调用__destruct
,但我无法轻易地重现它。有人注意到过吗?
编辑
我会在这里发布整个内容,这是Symfony的测试环境,如果你熟悉该框架,那么你可以很容易地重现它:require_once dirname(__FILE__).'/../bootstrap/Doctrine.php';
$profiler = new Doctrine_Connection_Profiler();
$conn = Doctrine_Manager::connection();
$conn->setListener($profiler);
$t = new lime_test(0, new lime_output_color());
class MyDestructableClass {
function __construct() {
print "\nIn constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "\nDestroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
$news = new News();
$news->setUrl('http://test');
$news->setHash('http://test');
$news->setTitle('http://test');
$news->setSummarize('http://test');
$news->setAccountId(1);
$news->setCategoryId(1);
$news->setThumbnail('http://test');
$news->setCreatedAt(date('Y-m-d H:i:s',time()));
$news->setUpdatedAt(date('Y-m-d H:i:s',time()));
$news->save();
exit();
__CLASS__
。 - Frank Farmerget_class()
是一个棘手的函数,如果你不了解它的特性,它可能会表现得非常出乎意料。例如,如果你在一个非对象(null)上调用get_class()
,它将返回当前类的名称,非常令人困惑。我甚至建议完全抵制这个函数。 - Daniel W.