In PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory. This is the garbage disposal mechanism of php gc (GarbageCollector) to prevent memory overflow.
In PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory. This is the Garbage disposal mechanism of php gc (Garbage Collector) to prevent memory overflow.
Destructor: it is executed when an object becomes junk or when the object is explicitly destroyed.
GC (Garbage Collector)
In PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory.
This is the Garbage disposal mechanism of php gc (Garbage Collector) to prevent memory overflow.
When a PHP thread ends, all currently occupied memory space will be destroyed, and all objects in the current program will be destroyed.
_ Destruct () destructor
The _ destruct () destructor is executed when the garbage object is recycled.
Destructor can also be explicitly called, but do not.
Destructor are automatically called by the system. Do not call a fictitious function of an object in a program.
The Destructor cannot contain parameters.
As shown in the following program, all objects are destroyed before the program ends. The Destructor is called.
The Code is as follows:
Class Person {
Public function _ destruct (){
Echo 'destructor is now executed
';
Echo 'here is generally used to set, close the database, close the file, and so on ';
}
}
$ P = new Person ();
For ($ I = 0; $ I <5; $ I ++ ){
Echo "$ I
";
}
?>
Program running result:
0
1
2
3
4
The Destructor is now executed.
It is generally used to set, close the database, close the file, and so on.
If the object is not pointed to, the object is destroyed.
The Code is as follows:
Class Person {
Public function _ destruct (){
Echo 'destructor is now executed
';
}
}
$ P = new Person ();
$ P = null; // The Destructor is executed here
$ P = "abc"; // same effect
For ($ I = 0; $ I <5; $ I ++ ){
Echo "$ I
";
}
?>
Program running result:
The Destructor is now executed.
0
1
2
3
4
In the above example, for the row 10th, we set $ p to null or the row 11th to assign $ p a string, so that the object previously pointed to by $ p becomes a junk object. PHP destroys the object garbage.
Php unset variable
The Code is as follows:
Class Person {
Public function _ destruct (){
Echo 'destructor is now executed
';
}
}
$ P = new Person ();
$ P1 = $ p;
Unset ($ p );
Echo: Now $ p has been destroyed. Is the object also destroyed?
';
For ($ I = 0; $ I <5; $ I ++ ){
Echo "$ I
";
}
Echo: Now $ p1 is destroyed, and no variable pointing to the object has been found.
';
Unset ($ p1); // No variable pointing to the object now. The Destructor is executed here.
?>
Program running result:
Now $ p is destroyed, and the object is also destroyed?
0
1
2
3
4
Now, $ p1 is destroyed, and no variable pointing to the object has been found.
The Destructor is now executed.
Unset destroys the variable pointing to the object, not the object.