我想知道魔术方法__destruct()
的行为,所以我写了一个小测试用例,但没有给出我期望的结果。有人可以为我澄清一下吗?
我一直认为exit()
会阻止PHP执行,在这种情况下无法调用__destruct()
。
PHP 7.1.8-2+ubuntu14.04.1+deb.sury.org+4 (cli) (built: Aug 4 2017 14:30:23) ( NTS )
class DestructorTest
{
public function __construct()
{
print "Constructed\n";
}
public function doSomething()
{
print "Doing something\n";
exit("Exiting...\n");
}
public function somethingElse()
{
print "Doing something else\n";
}
public function __destruct()
{
print "Destructing\n";
$this->somethingElse();
exit("Final exit\n");
}
}
$destruct = new DestructorTest();
$destruct->doSomething();
Constructed
Doing something
Exiting...
Destructing
Doing something else
Final exit