PHP的类对象在什么时候销毁呢?该如何解决

WBOY
Release: 2016-06-13 13:53:14
Original
2097 people have browsed it

PHP的类对象在什么时候销毁呢?
有个问题,对象在什么时候会自动销毁,脚本执行完毕当然就自动销毁了,可是如果我在一个函数里面创建了一个对象a,那么当这个函数执行完毕时,对象a会自动销毁么?

------解决方案--------------------
参考:
http://school.itzcn.com/special-spid-30.html
上面讲解的比较详细,
希望对楼主有所帮助。
------解决方案--------------------
准确地说,是在垃圾回收器运行之后回收.....

一般情况下没问题,函数结束后会回收...


不过有兴趣的可以运行一下这个程序: (

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 $a = new A();
//$a->__destruct(); // 看看这行注释与不注释有何不同
}
}

test();
echo number_format(memory_get_usage());


注释和不注释上面那行的结果是完全不同的



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template