Php 有没有任何方法可以在不逐行分配null的情况下使所有$this变量为null?
有没有任何方法可以在不逐行分配null的情况下使所有$this变量为nullPhp 有没有任何方法可以在不逐行分配null的情况下使所有$this变量为null?,php,class,variables,this,Php,Class,Variables,This,有没有任何方法可以在不逐行分配null的情况下使所有$this变量为null public function refresh(){ unset($this); } 取消设置不起作用。您不能在$this中取消设置($th);否则你将一事无成 $this->propertyA = $this->propertyB = $this->propertyC = NULL; 你为什么要这么做?简单地取消设置您正在使用的类的实例,其中的所有内容都将消失 $a = new S
public function refresh(){
unset($this);
}
取消设置不起作用。您不能在$this中取消设置($th);否则你将一事无成
$this->propertyA = $this->propertyB = $this->propertyC = NULL;
你为什么要这么做?简单地取消设置您正在使用的类的实例,其中的所有内容都将消失
$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;
}
您可以尝试循环遍历每个字段并逐个取消设置它们。像这样:
function refresh() {
foreach($this as $key => $value) {
unset($this->$key);
}
}
注意:我现在无法测试此代码。此代码未经测试。类在访问方面不是数组。