php - 重置/清除对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3845051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:18:14  来源:igfitidea点击:

php - reset/clear an object?

php

提问by Joe

What I'm trying to do is use a temporary object to store values and then reset it back to empty without having to uset($tmpObject); ?

我想要做的是使用一个临时对象来存储值,然后将其重置为空而不必使用 uset($tmpObject); ?

Here is some example code:

下面是一些示例代码:

class Object {
    function ResetObject(){
        // code to remove all variables in an object here?
    }
}

$tmpObject = new Object();

foreach ($myArray as $arr){
    $tmpObject->val1 = "string1";
    $tmpObject->val2 = "string2";
    $tmpObject->val3 = "string3";
    $tmpObject->val4 = "string4";       
    $template->set('tmpObject',$tmpObject);
    $tmpObject->ResetObject();
}

Anyone have any ideas?

谁有想法?

回答by NikiC

class Object {
    function ResetObject() {
        foreach ($this as $key => $value) {
            unset($this->$key);
        }
    }
}

See: Object iteration

请参阅:对象迭代

回答by Matt Browne

The accepted answer has a minor flaw which is that unsettinga property actually completely removes it from that object, so that a check like $this->someProperty == nullwould trigger an "Undefined property" notice. Properties are null by default, so this would be more correct:

接受的答案有一个小小的瑕疵这是你重置一个属性实际上完全从该对象中删除,因此像一张支票$this->someProperty == null将触发一个“未定义的属性”的通知。默认情况下,属性为 null,所以这会更正确:

class Object {
    function resetObject() {
        foreach ($this as $key => $value) {
            $this->$key = null;  //set to null instead of unsetting
        }
    }
}

There's also the possibility that some of the properties could have been given default values (e.g. protected $someArray = array();)...if you want to reset all the properties back to their original default values then you have to use reflection:

还有一种可能性,某些属性可能已经被赋予默认值(例如protected $someArray = array();)...如果您想将所有属性重置回其原始默认值,那么您必须使用反射:

class Object {
    function resetObject() {
        $blankInstance = new static; //requires PHP 5.3+  for older versions you could do $blankInstance = new get_class($this);
        $reflBlankInstance = new \ReflectionClass($blankInstance);
        foreach ($reflBlankInstance->getProperties() as $prop) {
            $prop->setAccessible(true);
            $this->{$prop->name} = $prop->getValue($blankInstance);
        }
    }
}

That may be overkill but could be important in some scenarios. Note that this would fail if the class had required constructor arguments; in that case you could use ReflectionClass::newInstanceWithoutConstructor(introduced in PHP 5.4), then call __construct()manually after calling resetObject().

这可能有点矫枉过正,但在某些情况下可能很重要。请注意,如果类需要构造函数参数,这将失败;在这种情况下,您可以使用ReflectionClass::newInstanceWithoutConstructor(在 PHP 5.4 中引入),然后__construct()在调用resetObject().

回答by Spudley

If your class has a constructor method which initialises everything, then you could just call that again to reset.

如果你的类有一个初始化所有东西的构造函数方法,那么你可以再次调用它来重置。

回答by user2809280

<?php  
function reset() {
    foreach (get_class_vars(get_class($this)) as $var => $def_val){
        $this->$var= $def_val;
    }
}
?>

回答by CagunA

Another way which combine unset and set with default value.

另一种结合未设置和设置与默认值的方式。

class Object {
    public function resetObject()
    {
        $clean = new self;
        foreach ($this as $key => $val){

            // If the attribute have a default value, use it 
            if (isset($clean->$key)){
                $this->$key = $clean->$key;
            }else{
                unset($this->$key);
            }
        }
    }
}

回答by Propeng

Reinitializing the variable will restore all object members to their preset values.

重新初始化变量会将所有对象成员恢复为其预设值。

<?php

class Object {
  public $val1 = "val1";
  public $val2 = "val2";
}

$tmpObject = new Object();

$tmpObject->val1 = "string1";
$tmpObject->val2 = "string2";
$tmpObject->val3 = "string3";
$tmpObject->val4 = "string4";

var_dump($tmpObject);
$tmpObject = new Object();
var_dump($tmpObject);

?>

Outputs:

输出:

object(Object)#1 (4) {
  ["val1"]=>
  string(7) "string1"
  ["val2"]=>
  string(7) "string2"
  ["val3"]=>
  string(7) "string3"
  ["val4"]=>
  string(7) "string4"
}
object(Object)#2 (2) {
  ["val1"]=>
  string(4) "val1"
  ["val2"]=>
  string(4) "val2"
}