Php destructor _ destruct: Under what conditions is the execution of all conditions met? & Amp; gt; php Terminator also executes php destructor _ destruct under what circumstances
Find all the situations, right?> Php Terminator is also executed
Reply content:
Php destructor _ destruct when to execute
Find all the situations, right?> Php Terminator is also executed
RTFM
Http://php.net/manual/en/language.oop5.decon.php#object.destruct
The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
Run the command before the object is destroyed?
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 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 result is as follows:
unset $c2unset $c1sf::__destructnull $c2null $c1sf::__destruct123 $c2456 $c1sf::__destructthe end
The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
Generally, at the end of the script (rather than unset), php will destroy the reference and run it before the script stops running.
Exit (); also executed. It hurts.