---
title: "Object Cloning in PHP"  
description: "Objects are inconstant, that means object has capability to change the state of information. In object oriented PHP programming, there can be a situat"  
author: "Anonymous User"  
published: 2016-06-21  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11163/object-cloning-in-php  
category: "php"  
tags: ["php"]  
reading_time: 4 minutes  

---

# Object Cloning in PHP

Objects are **inconstant**, that means object has capability to change the state of information. In [object oriented](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s) PHP [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-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](https://www.mindstick.com/interview/2374/what-is-object-cloning-in-java). 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](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) 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](https://answers.mindstick.com/qa/51749/what-is-sse-in-html5-and-also-describe-the-concept-how-to-automatically-gets-update-a-webpage-from-a-server) 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 Raginiecho $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

```
<?phpclass 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](https://www.mindstick.com/interview/2390/can-you-achieve-runtime-polymorphism-by-data-members-in-java) 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](https://www.mindstick.com/forum/156160/what-is-google-knowledge-graph) about magical method.

The [magic methods](https://www.mindstick.com/interview/23070/what-are-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](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) are always defined inside the class and are not defined [outside the class](https://www.mindstick.com/interview/2468/can-you-access-the-private-method-from-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:

```
<?phpclass 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.

---

Original Source: https://www.mindstick.com/blog/11163/object-cloning-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
