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.
__destruct () destructor
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 fictional function of an object in a program.
destructors cannot have parameters.
All objects are destroyed before the program ends, as shown in the following procedure. The destructor is called.
";}? >
Program Run Result:
0 1 2 3 4 The destructor now executes the finishing touches of setting up, shutting down the database, closing files, and so on.
When the object is not pointing, the object is destroyed.
";}? >
Program Run Result:
In the above example, line 10th, we set $p to null or line 11th to give $p a string, so that the object pointed to $p is a garbage object. PHP destroys this object garbage.
PHP unset variables
';}} $p = new Person (), $p 1 = $p; unset ($p); Echo ' is now destroying $p, has the object been destroyed?
";} Echo ' Now destroys $p 1, which is no longer a variable that points to the object.
'; unset ($p 1); There is no variable pointing to the object, and the destructor executes the?> here.
Program Run Result:
Unset destroys a variable that points to an object, not the object.
http://www.bkjia.com/PHPjc/752488.html www.bkjia.com true http://www.bkjia.com/PHPjc/752488.html techarticle destructor : Executes when an object becomes garbage or when an object is explicitly destroyed. GC (Garbage Collector) in PHP, when no variable points to this object, the object becomes ...