在 PHP5 中自动取消 _destruct 中所有类变量的设置
我有一个类,其中的某些方法中设置了类变量。该类有一个 __destruct() 函数,可以使用 unset() 函数取消设置类变量。
现在我列出了 __destruct 中要取消设置的所有变量,但似乎应该有一种方法可以取消所有设置。
例如,现在我正在这样做:
function __destruct()
{
unset($this->variable1);
unset($this->variable2);
//et cetera
}
当然有办法取消它们全部而不列出它们,对吧?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 http://www.php.net/manual/en/language。 oop5.iterations.php。
您不应该
取消设置
属性,它们是类/对象的一部分。将它们设置为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 tonull
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.