Home > DeveloperSection > Blogs > Object Cloning in PHP

Object Cloning in PHP


PHP PHP 
Ratings:
0 Comment(s)
 130  View(s)
Rate this:

Object Cloning in PHP


Objects are inconstant, that means object has capability to change the state of information. In object oriented PHP programming, there can be a situation where you require copies of object. We can create copy of an object using” =” operator than why use object cloning. Following is an example which shows how ‘=’operator works for creating copies of an object:


When you have created a copy of an object using “=” operator. The reference of both the object (original and the one which is created) has the same reference. After copying, when you will change the value of the variable of one object than other object will automatically gets changed. Following is an example which shows how variable of one object gets changed:

<?php                                  

class employee {

          private $name;

 

          public function text($emp_name) {

                   $this->name = $emp_name;

          }

 

          public function Name() {

                   return $this->name;

          }

}

 

$obj1 = new employee();

$obj1->text("Neha");

 

$obj2 = $obj1; //only reference or memory assigned to $obj2

 

$obj2->text("Ragini");

 

echo $obj1->Name()."</br>";  // Neha will be replaced by Ragini

echo $obj2->Name();

?>

 

Output

Ragini   // $obj1 displays Ragini

Ragini  // $obj2 also display Ragini

 

As in the above example, we can say that when the object is copied directly it is copied by reference, not by value. It means when you have changed the main object value then copied object is also gets affected and also when you will change the value of copied object then the main object value will also gets changed. This problem is solved by object cloning in which object will never have the reference of main object or original object. In this section we will explain how to make a clone of an object and how the value of original object and cloned object gets changed.


To clone an object means to create a similar object with different reference. Following is an example a clone object is shown using keyword clone

<?php

class employee {

          private $name;

 

          public function text($emp_name) {

                   $this->name = $emp_name;

          }

 

          public function Name() {

                   return $this->name;

          }

}

 

$obj1 = new employee();

$obj1->text("Neha");

 

$obj2 = clone $obj1;           //only reference or memory assigned to $obj2

 

$obj2->text("Ragini");

 

echo $obj1->Name()."</br>";

echo $obj2->Name();

?>

 

Output

Neha       // $obj1 displays Neha

Ragini    // $obj2 also display Ragini

 

From the above example you can see $obj1 gets different reference in which Neha value gets displayed and in $obj2 have different reference in which Ragini value gets displayed.


Note: Above technique will only work if data members are intrinsic type like Boolean, string, float, int, etc. However, this will not work with data member which is an object of another class. This problem is solved by __clone magical method.

Object Cloning using magic method

Object cloning can also be done using __clone magical method. Before we go further on __clone method we will give you a brief knowledge about magical method.


The magic methods are one with special names which are executed in response to some events. These magic methods are predefined function by PHP compiler, starting with two underscore “__”, which indicate methods, which will be triggered by some PHP events. These functions are always defined inside the class and are not defined outside the class (they are not standalone functions). For further details please go to our previous article magical method.

 Following is an example to show __clone() method:

<?php

class employee {

          private $name;

 

          public function text($emp_name) {

                   $this->name = $emp_name;

          }

 

          public function Name() {

                   return $this->name;

          }

          public function __clone() {

                   $obj1 = new employee();

                   $obj1->text($this->name);

                   return $obj1;

          }

}

 

$obj1 = new employee();

$obj1->text("Neha");

 

$obj2 = clone $obj1; //only reference or memory assigned to $c2

 

$obj2->text("Ragini");

 

echo $obj1->Name()."</br>";

echo $obj2->Name();

?>

 

Output

Neha       // $obj1 displays Neha

Ragini    // $obj2 also display Ragini

 

From the above example you can see $obj1 gets different reference in which Neha value gets displayed and in $obj2 have different reference in which Ragini value gets displayed.


Don't want to miss updates? Please click the below button!

Follow MindStick