加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP在函数结束后立即释放局部变量吗?

发布时间:2020-12-13 18:12:10 所属栏目:PHP教程 来源:网络整理
导读:代码更好地说明了我的要求: 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在某些时候会被释放,
代码更好地说明了我的要求:
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()

现在很清楚,变量在函数出口处被破坏了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读