php Destructor method Under what circumstances is destruct executed? In all cases, is it encountered?>php terminator is also executed
Destructor function Will be executed when all references to a certain object are deleted or when the object is explicitly destroyed
Executed before the object is destroyed Bar?
According to the official manual, the destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed. The code demonstration is as follows
class sf{ public function destruct() { echo METHOD . PHP_EOL; } } $c1 = new sf; $c2 = $c1;echo 'unset $c2' . PHP_EOL;unset($c2);echo 'unset $c1' . PHP_EOL;unset($c1);// ----$c1 = new sf; $c2 = $c1;echo 'null $c2' . PHP_EOL; $c2 = null;echo 'null $c1' . PHP_EOL; $c1 = null;// ----$c1 = new sf; $c2 = $c1;echo '123 $c2' . PHP_EOL; $c2 = 123;echo '456 $c1' . PHP_EOL; $c1 = 456;echo 'the end' . PHP_EOL;
The running results are as follows
unset $c2unset $c1 sf::destructnull $c2null $c1 sf::destruct123 $c2456 $c1 sf::destruct the end
The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed.
Generally speaking, php will destroy the reference when the script ends (not unset) and runs before the script ends.
The above is the detailed content of PHP: Under what circumstances is the destructor method __destruct executed?. For more information, please follow other related articles on the PHP Chinese website!