代码更好地说明了我的要求:
- function foo(){
- $var = get_huge_amount_of_data();
- return $var[0];
- }
- $s = foo();
- // is memory freed here for the $var variable created above?
- do_other_stuff(); // need memory here lol
所以我知道$var在某些时候会被释放,但PHP是否有效地做到了?或者我手动需要取消设置昂贵的变量?
你可以在类上看到这个例子,因为你可以“捕获”释放类’析构函数中的变量:
- class a {
- function __destruct(){
- echo "destructor<br>";
- }
- }
- function b(){ // test function
- $c=new a();
- echo 'exit from function b()<br>';
- }
- echo "before b()<br>";
- b();
- echo "after b()<br>";
- die();
这个脚本输出:
- before b()
- exit from function b()
- destructor
- after b()
现在很清楚,变量在函数出口处被破坏了.