php - 重置/清除对象?

时间:2010-10-02 09:02:55

标签: php

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

以下是一些示例代码:

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();
}

有人有什么想法吗?

8 个答案:

答案 0 :(得分:23)

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

请参阅:Object iteration

答案 1 :(得分:19)

接受的答案有一个小缺陷,即取消设置属性实际上完全将其从该对象中删除,因此像$this->someProperty == null这样的检查会触发“未定义属性”通知。默认情况下,属性为null,因此这更正确:

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

还有可能某些属性已被赋予默认值(例如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);
        }
    }
}

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

答案 2 :(得分:3)

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

<?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);

?>

输出:

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"
}

答案 5 :(得分:0)

将未设置和设置与默认值组合的另一种方法。

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);
            }
        }
    }
}

答案 6 :(得分:0)

将其设置为默认值可能会更好,而不是取消设置甚至设置为null

class Foo {
public $bar = 'default bar';
private $baz = 'something';

  function __construct ()
  {
   /* assign whatever */
  }

 public function resetMe():Foo
 {
   $instance = new Foo();
   foreach($instance as $k => $v)
      $this->{$k} = $v;
 }
 return $this;
}

要使用此

$foo = new Foo(); 
$foo->bar = 'set something different';
echo $foo->bar; // 'set something different'
$foo->resetMe();
echo $foo->bar; // 'default bar'

答案 7 :(得分:0)

你可以通过反射来做到这一点:

    private function resetPropertiesToDefault()
    {
        $blankInstance = $this->newInstance();
        $reflBlankInstance = new \ReflectionClass($blankInstance);
        foreach ($reflBlankInstance->getProperties() as $prop) {

            if ($prop->isStatic()) {
                continue;
            }

            $prop->setAccessible(true);

            $this->{$prop->name} = $prop->getValue($blankInstance);
        }
    }