Explanation of PHP destructor and recycling mechanism examples

Source: Internet
Author: User

This article mainly share with you the PHP destructor and recycling mechanism examples, mainly in the way of code and everyone to share, hope to help everyone.

= = = Note Part 1===

/*
Questions
1, the object is assigned to other, such as true will not destroy the object?
Answer: You can

2, 110.php code Part 4
Why destroy one, empty 2
The last one will appear below the HR line?

A: The last one was destroyed because the PHP page was executed,
Finally, the system is recycled, $d destroyed at this time,
So it appears behind the HR line
*/


= = = Code section 1===

Class Human2 {public    $name = null;    public $gender = null;    Public Function __construct () {        echo ' was born <br > ';    }    Public Function __destruct () {        echo ' goodbye <br > ';    }}  $a = new Human2 (), $b = new Human2 (), $c = new Human2 (), $d = new Human2 (), unset ($a),//$b = false, $b = true;//converted to True can also be destroyed $c = Null;echo ' 



Recycling mechanism for objects

= = = Code section 2===

Class Human {public    $name = null;    public $gender = null;    Public Function __destruct () {        echo ' Goodbye! <br > ';    }} $a = new Human (), $b = $c = $d = $a; unset ($a); Echo ' 

/*
So here's the question:
1. How many times have you died?
2, die on the HR line, or die in the HR line?

A: Dead once, below the gray line.
As shown in 11101, one of the keys to the human house has been opened.
and b,c,d three keys.

Until all the code finishes executing, start the recycle mechanism,
Finally goodbye.
*/



= = = Code section 3===

Class Human {public    $name = ' Zhang San ';    public $gender = null;    Public Function __destruct () {        echo ' Goodbye! <br > ';    }} $a = new Human (), $b = $c = $d = $a; echo $a->name, ' <br > '; Zhang San Echo $b->name, ' <br > '; Zhang San $b->name = ' John Doe '; Echo $a->name, ' <br > '; John Doe Echo $b->name, ' <br > '; John Doe unset ($a); Echo ' 



= = = Code section 4===

Class Human {public    $name = ' Zhang San ';    public $gender = null;    Public Function __destruct () {        echo ' Goodbye! <br > ';    }} $e = $f = $g = new Human (); unset ($e); Echo ' unset e<br > '; unset ($f); Echo ' unset f<br > '; unset ($g);//The Bank unset triggers the Like extinct, then perform the next line of Echo Gecho ' unset g<br > ';

/*
Here, the page runs, the object is destroyed, and the destructor is executed.
How many objects have been destroyed?

For:
Only one object, only 1 times dead.
Die when the system is reclaimed, that is, the page executes, so it is under the HR line.
*/

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.