I have an object $objDummy
of some class ClassDummy
and another is as
$objClone = clone $objDummy;
Then I make any ch
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 = '
';
//*/
$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 = '
';
//*/
$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