如何在克隆操作期间修改对象的副本?

时间:2010-06-22 23:56:36

标签: php clone

如何在克隆操作期间修改对象的副本?

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

class MyClass {

    private $myArray = array();

    public function pushSomethingToArray($var) {
        array_push($this->myArray, $var);
    }   //  function pushSomethingToArray()

    public function getArray() {
        return $this->myArray;
    }   //  function getArray()

    public function __clone()
    {
        //  clear array
        $this->myArray = array();
    }   //  function __clone()
}

$myObj = new MyClass();
$myObj->pushSomethingToArray('blue');
$myObj->pushSomethingToArray('orange');

$myObjClone = clone $myObj;
$myObjClone->pushSomethingToArray('red');

var_dump($myObj->getArray());
echo '<br />';
var_dump($myObjClone->getArray());

答案 2 :(得分:0)

$clone = clone $object;
modify($clone);

虽然如此,但在克隆操作之后;我不知道在操作期间如何做到(如果可能的话)