函数运行后函数中的变量会被销毁吗?
class B {
function C() {
$x = "123456";
echo "XXX".$x;
// After this function is finished, will $x be destroyed by default to save memory in PHP?
}
}
class A {
function F1() {
return new Class_B();
}
function F2() {
$this->F1()->C();
// After this function is finished, will F1 be destroyed by default to save memory and CPU in PHP?
}
}
关于$x
:是的,B::C()
运行完成后,它将受到垃圾回收。
关于$this->F1()->C()
:F1
方法本身不会被销毁,但它返回的B
实例将在F2
运行完毕后销毁。
是的,它将被"销毁",以便可以重复使用它占用的内存。