为什么exit()不会阻止调用__destruct()?

时间:2017-10-24 14:53:01

标签: php oop destructor

我想知道魔术方法__destruct()的行为,所以我写了一个小测试用例,但没有给出我期望的结果。有人可以为我澄清一下吗?

我一直认为exit()会阻止PHP执行,在这种情况下无法调用__destruct()

PHP版

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

0 个答案:

没有答案