A detailed explanation of the garbage collection mechanism of the destructor and PHP _php tutorial

Source: Internet
Author: User
Destructors :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.
Copy the Code code as follows:
Class Person {
Public Function __destruct () {
The Echo ' destructor now executes
';
Echo ' is used here to set up, close the database, close files and other finishing tasks ';
}
}

$p = new Person ();
for ($i = 0; $i < 5; $i + +) {
echo "$i
";
}

?>

Program Run Result:
0
1
2
3
4

The destructor now executes
This is usually used to set up, close the database, close files and other finishing tasks

When the object is not pointing, the object is destroyed.
Copy the Code code as follows:
Class Person {
Public Function __destruct () {
The Echo ' destructor now executes
';
}
}

$p = new Person ();
$p = null; The destructor is executed here.
$p = "ABC"; The same effect
for ($i = 0; $i < 5; $i + +) {
echo "$i
";
}

?>

Program Run Result:

The destructor now executes
0
1
2
3
4

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
Copy the Code code as follows:

Class Person {
Public Function __destruct () {
The Echo ' destructor now executes
';
}
}

$p = new Person ();
$p 1 = $p;

Unset ($p);
Echo ' has now destroyed $p, has the object been destroyed?
';

for ($i = 0; $i < 5; $i + +) {
echo "$i
";
}

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 is executed here.

?>

Program Run Result:

Now that the $p have been destroyed, has the object been destroyed?
0
1
2
3
4

Now the $p 1 is also destroyed, that there is no point to the object of the variable

The destructor now executes

Unset destroys a variable that points to an object, not the object.

http://www.bkjia.com/PHPjc/824907.html www.bkjia.com true http://www.bkjia.com/PHPjc/824907.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 ...

  • 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.