Destructor: Executes when an object becomes garbage or when an object is explicitly destroyed.
GC (Garbage Collector)
In PHP, when there are no variables pointing to this object, the object becomes garbage. PHP will destroy it in memory. This is the PHP gc (garbage Collector) garbage disposal mechanism to prevent memory overflow. When a PHP thread ends, all memory space currently occupied is destroyed, and all objects in the current program are destroyed.
The destruct () destructor is executed when the garbage object is reclaimed.
Destructors can also be called explicitly, but do not do so.
Destructors are automatically called by the system, and do not call a destructor for an object in the program.
destructors cannot have parameters.
All objects are destroyed before the program ends. The destructor is called.
<?php class person{public function destruct () { echo "<br/> destructor is executed here"; echo "<br/> Here is generally used to place, close the database, close files and so on finishing work. "; } } $p = new Person (); for ($i = 0; $i < 5; $i + +) { echo "<br/> $i"; } We see here that the object was destroyed before the PHP program was finished. ?>
When the object is not pointing, the object is destroyed.
<?php class Person {public function destruct () { echo "<br/> destructor is executed here"; echo "<br/> Here is generally used to place, close the database, close files and so on finishing work. "; } } $p = new Person (); $p = null; As we can see here, destructors are executed here. For ($i = 0; $i < 5; $i + +) { echo "<br/> $i"; } ? >
We set the $p to null or the 11th line gives $p a string so that the object pointed to by $p becomes a garbage object. PHP destroys this object garbage.
unset variable
<?php class Person {public function destruct () { echo "<br/> destructor is executed here <br/>"; } } $p = new Person (); $p 1 = $p; Set the new reference variable $P1 also points to this object unset ($p); Did echo "See/$p be destroyed and objects destroyed?"; for ($i = 0; $i < 5; $i + +) { echo "<br/> $i"; } Unset ($p 1); echo "We see here, destructors are executed"; ? >
When
unset a reference variable, unset destroys the variable that points to the object, not the object.