Explanation of destructor and php garbage collection mechanism

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.