2

我有一个类,其中在某些方法中设置了类变量。该类有一个 __destruct() 函数,该函数使用 unset() 函数取消设置类变量。

现在我列出了所有要在 __destruct 中取消设置的变量,但似乎应该有一种方法可以取消所有设置。

例如,现在我正在这样做:

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

当然有办法在不列出它们的情况下将它们全部取消,对吧?

4

1 回答 1

7
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.

于 2011-07-07T01:58:07.577 回答