PHP Object Cloning

PHPServer Side ProgrammingProgramming

Introduction

Creating copy of an object by simple assignment merely creates another reference to the one in memory. Hence, changes in attribute reflect both in original and duplicate object. PHP has clone keyword that creates a shallow copy of the object. However, if original object has other embedded object as one of its properties, the copied object still refers to the same. To create a eep copy of object, the magic method __clone() needs to be defined in the class/

Copy by assignment

In following code, myclass has one of attributes as object of address class. An object of myclass is duplicated by assignment. Change in value of its prroperty is reflected in both objects

Example

 Live Demo

<?php
class address{
   var $city="Nanded";
   var $pin="431601";
   function setaddr($arg1, $arg2){
      $this->city=$arg1;
      $this->pin=$arg2;
   }
}
class myclass{
   var $name="Raja";
   var $obj;
   function setname($arg){
      $this->name=$arg;
   }
}
$obj1=new myclass();
$obj1->obj=new address();
echo "original object\n";
print_r($obj1);
$obj2=$obj1;
$obj1->setname("Ravi");
echo "after change:\n";
print_r($obj1);
print_r($obj2);
?>

Output

This code shows following output

original object
myclass Object(
   [name] => Raja
   [obj] => address Object(
      [city] => Nanded
      [pin] => 431601
   )
)
after change:
original object
myclass Object(
   [name] => Ravi
   [obj] => address Object(
      [city] => Nanded
      [pin] => 431601
   )
)
copied object
myclass Object(
   [name] => Ravi
   [obj] => address Object(
      [city] => Nanded
      [pin] => 431601
   )

)

Using clone keyword

The clone keyword creates a shallow copy. Change in value of property doesn't reflect in cloned object. However, if embedded object is modified, changes are reflected in original and cloned object

Example

 Live Demo

<?php
class address{
   var $city="Nanded";
   var $pin="431601";
   function setaddr($arg1, $arg2){
      $this->city=$arg1;
      $this->pin=$arg2;
   }
}
class myclass{
   var $name="Raja";
   var $obj;
   function setname($arg){
      $this->name=$arg;
   }
}
$obj1=new myclass();
$obj1->obj=new address();
echo "original object:\n";
print_r($obj1);
$obj2=clone $obj1;
$obj1->setname("Ravi");
$obj1->obj->setaddr("Mumbai", "400001");
echo "after change:\n";
echo "original object:\n";
print_r($obj1);
echo "cloned object:\n";
print_r($obj2);
?>

Output

Output shows following result

original object:
myclass Object(
   [name] => Raja
   [obj] => address Object(
      [city] => Nanded
      [pin] => 431601
   )

)
after change:
original object:
myclass Object(
   [name] => Ravi
   [obj] => address Object(
      [city] => Mumbai
      [pin] => 400001
   )

)
cloned object:
myclass Object(
   [name] => Raja
   [obj] => address Object(
      [city] => Mumbai
      [pin] => 400001
   )

)

Using __clone() method

The __clone() method creates a deep copy by creating lone of embedded object too

Example

<?php
class address{
   var $city="Nanded";
   var $pin="431601";
   function setaddr($arg1, $arg2){
      $this->city=$arg1;
      $this->pin=$arg2;
   }
}
class myclass{
   var $name="Raja";
   var $obj;
   function setname($arg){
      $this->name=$arg;
   }
   public function __clone() {
      $this->obj = clone $this->obj ;
   }
}
$obj1=new myclass();
$obj1->obj=new address();
echo "original object:\n";
print_r($obj1);
$obj2=clone $obj1;
$obj1->setname("Ravi");
$obj1->obj->setaddr("Mumbai", "400001");
echo "after change:\n";
echo "original object:\n";
print_r($obj1);
echo "cloned object:\n";
print_r($obj2);
?>

Output

Output shows following result

original object:
myclass Object(
   [name] => Raja
   [obj] => address Object(
      [city] => Nanded
      [pin] => 431601
   )

)
after change:
original object:
myclass Object(
   [name] => Ravi
   [obj] => address Object(
      [city] => Mumbai
      [pin] => 400001
   )

)
cloned object:
myclass Object(
   [name] => Raja
   [obj] => address Object(
      [city] => Nanded
      [pin] => 431601
   )

)
raja
Published on 18-Sep-2020 13:22:11
Advertisements