Concrete analysis of destructor and garbage collection in PHP

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

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.