Automatically Unsetting All Class Variables in _destruct in PHP5
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.
精彩评论