When does the class object of PHP be destroyed? How to fix it
Source: Internet
Author: User
When does the class object of PHP be destroyed?
There is a problem, when the object is automatically destroyed, the execution of the script is automatically destroyed, but if I create an object in a function of a, then when the function is finished, object A will automatically destroy it?
------Solution--------------------
Reference:
Http://school.itzcn.com/special-spid-30.html
The above explanation is more detailed,
Hope that the landlord has helped.
------Solution--------------------
To be exact, it is recycled after the garbage collector is running .....
Generally no problem, the function will be recycled after the end of ...
But interested can run this program: (
Class A {
function __construct () {
$this->b = new B ($this);
}
function __destruct () {
$this->b->__destruct ();
}
}
Class B {
function __construct ($parent = NULL) {
$this->parent = $parent;
}
function __destruct () {
unset ($this->parent);
}
}
function Test () {
for ($i = 0; $i < 10000; $i + +) {
$a = new A ();
$a->__destruct (); See how this line of comments differs from not commenting
}
}
Test ();
Echo Number_format (Memory_get_usage ());
Comments and not commenting the result of the above line is completely different.
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.