Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone is not working for me?

Tags:

clone

php

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
like image 215
Gaurav Avatar asked Dec 21 '22 18:12

Gaurav


1 Answers

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
like image 112
Rich Adams Avatar answered Jan 08 '23 16:01

Rich Adams