I was wondering about the behavior of the magic method __destruct()
and so I wrote a little test case that doesn't give me the outcome I would expect. Can someone clarify this for me?
I always thought that exit()
would stop PHP from executing and in this case wouldn't be able to call __destruct()
.
PHP Version
PHP 7.1.8-2+ubuntu14.04.1+deb.sury.org+4 (cli) (built: Aug 4 2017 14:30:23) ( NTS )
Code
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();
Output
Constructed
Doing something
Exiting...
Destructing
Doing something else
Final exit