开发者

Automatically Unsetting All Class Variables in _destruct in PHP5

开发者 https://www.devze.com 2023-03-18 08:43 出处:网络
I have a class in which class variables are set within some of the methods.The class has a __destruct() function that unsets class variables using the unset() function.

I have a class in which class variables are set within some of the methods. The class has a __destruct() function that unsets class variables using the unset() function.

Right now I am listing all variables to unset in __destruct, but it seems like there should be a way开发者_开发百科 to unset all.

For example, right now I am doing this:

function __destruct()
{
  unset($this->variable1);
  unset($this->variable2);
  //et cetera
}

Surely there's so way to unset them ALL without listing them, right?


foreach ($this as &$value) {
    $value = null;
}

See http://www.php.net/manual/en/language.oop5.iterations.php.

You should not unset properties, they're part of the class/object. Set them to null instead to clear their values. But: the object is about the go out of memory anyway, and all properties will go with it. There's no real need to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号