Clone is not working for me?

不问归期 提交于 2019-12-29 01:50:11

问题


I have an object $objDummy of some class ClassDummy and another is as

$objClone = clone $objDummy;

Then I make any change in $objClone, $objDummy is also changed. I do not want to do that. How can I get this to work?

EDIT: In response to Chris. Here is an example

<?php
class myAnotherObject{
    public $myAnotherVar =10;
}

class myObject {
    public $myVar = false;
    function __construct() {
        $this->myVar = new myAnotherObject();
    }
}


$nl = "\n";
//*
$nl = '<br />';
//*/


$obj1 = new myObject();
echo 'obj1->myVar->myAnotherVar: '.$obj1->myVar->myAnotherVar;

$obj2 = clone $obj1;

echo $nl.'obj1->myVar->myAnotherVar: '.$obj1->myVar->myAnotherVar.', obj2->myVar->myAnotherVar: '.$obj2->myVar->myAnotherVar;

$obj2->myVar->myAnotherVar = 20;
echo $nl.'obj1->myVar->myAnotherVar: '.$obj1->myVar->myAnotherVar.', obj2->myVar->myAnotherVar: '.$obj2->myVar->myAnotherVar;

The output is

obj1->myVar->myAnotherVar: 10
obj1->myVar->myAnotherVar: 10, obj2->myVar->myAnotherVar: 10
obj1->myVar->myAnotherVar: 20, obj2->myVar->myAnotherVar: 20

回答1:


Are you implementing the __clone() method? The examples in the PHP documentation on cloning will explain this better than I possibly can. Specifically you're interested in this part,

When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.

Once the cloning is complete, if a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.

UPDATE
Based on your update to the question, you're indeed missing the implementation of __clone(). Since the $myVar member of myObject is itself an object, you need to clone that as well. Here's what your myObject class should look like,

class myObject {
    public $myVar = false;
    function __construct() {
        $this->myVar = new myAnotherObject();
    }

    function __clone() {
        $this->myVar = clone $this->myVar;
    }
}

The output then becomes the following,

obj1->myVar->myAnotherVar: 10
obj1->myVar->myAnotherVar: 10, obj2->myVar->myAnotherVar: 10
obj1->myVar->myAnotherVar: 10, obj2->myVar->myAnotherVar: 20



回答2:


I am not able to replicate your results. Using the code below, I get the expected output (copied below the code). The ___clone() method is not required for this, as illustrated. Can you post a slimmed-down version of your code?

CODE

class myObject {
    public $myVar = false;
    function __construct($newVar=5) {
        $this->myVar = $newVar;
    }
}

$nl = "\n";
//*
$nl = '<br />';
//*/


$obj1 = new myObject(10);
echo 'obj1->myVar: '.$obj1->myVar;

$obj2 = clone $obj1;
echo $nl.'obj1->myVar: '.$obj1->myVar.', obj2->myVar: '.$obj2->myVar;

$obj1->myVar = 20;
echo $nl.'obj1->myVar: '.$obj1->myVar.', obj2->myVar: '.$obj2->myVar;

OUTPUT

obj1->myVar: 10
obj1->myVar: 10, obj2->myVar: 10
obj1->myVar: 20, obj2->myVar: 10

Edited after discussion:

Your issue is caused by a reference to an object. Since all objects are handled by reference, when you clone an object you also need to clone any internally held objects, otherwise you end up with a reference to a single object.

CODE

class myAnotherObject{
    public $myAnotherVar =10;
}

class myObject {
    public $myVar = false;
    function __construct() {
        $this->myVar = new myAnotherObject();
    }
    function __clone() {
        $this->myVar = clone $this->myVar;  
    }
}


$nl = "\n";
//*
$nl = '<br />';
//*/


$obj1 = new myObject();
echo 'obj1->myVar->myAnotherVar: '.$obj1->myVar->myAnotherVar;

$obj2 = clone $obj1;

echo $nl.'obj1->myVar->myAnotherVar: '.$obj1->myVar->myAnotherVar.', obj2->myVar->myAnotherVar: '.$obj2->myVar->myAnotherVar;

$obj2->myVar->myAnotherVar = 20;
echo $nl.'obj1->myVar->myAnotherVar: '.$obj1->myVar->myAnotherVar.', obj2->myVar->myAnotherVar: '.$obj2->myVar->myAnotherVar;

OUTPUT

obj1->myVar->myAnotherVar: 10
obj1->myVar->myAnotherVar: 10, obj2->myVar->myAnotherVar: 10
obj1->myVar->myAnotherVar: 10, obj2->myVar->myAnotherVar: 20


来源:https://stackoverflow.com/questions/4959949/clone-is-not-working-for-me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!