有没有什么方法可以使所有$this变量为空,而无需逐行赋值为null?
public function refresh(){
unset($this);
}取消设置不起作用。
发布于 2012-12-13 15:26:06
在$this中不能取消设置($ths);否则将一事无成。
$this->propertyA = $this->propertyB = $this->propertyC = NULL;发布于 2012-12-13 15:27:05
你为什么要这么做?只需取消设置您正在使用的类的实例,其中的所有内容都会消失
$a = new SomeClass();
$a->someVar = "1";
$a->someOtherVar = "2";
unset($a);但是不要忘记创建一个新的实例,如果你想继续$a,这会将所有的值重置为它们在属性定义中设置的默认值:
foreach (get_class_vars(get_class($this)) as $name => $def){
$this->$name = $def;
}发布于 2012-12-13 15:29:06
您可以尝试遍历每个字段并逐个取消设置它们。如下所示:
function refresh() {
foreach($this as $key => $value) {
unset($this->$key);
}
}注意:我现在不能测试它,这段代码是未经测试的。
https://stackoverflow.com/questions/13854959
复制相似问题