PHP Object Cloning

Object cloning is creating a copy of an object. An object copy is created by using the clone keyword and the __clone() method cannot be called directly. In PHP, cloning an object is doing a shallow copy and not a deep copy. Meaning, the contained objects of the copied objects are not copied. If you wish for a deep copy, then you need to define the __clone() method. When there is a need that you do not want the outer enclosing object to modify the internal state of the object then the default PHP cloning can be used. I will demonstrate object cloning using an example in this article.

PHP Object Cloning

Copy Objects by Assignment

In the below code, I am attempting to copy an object by using the assignment operator. So what happens is, the instance is just a pointer to the old instance. We can verify that by updating the values of its properties. When the values of the new instance are updated it gets reflected in the old instance. So, this type of copy is just a duplicate reference to the original instance. Technically this is not a copy, but it is just assigning the object’s reference to another object.

<?php
class Animals
{
public $name;
public $category;
}
//Creating instance of Animals class
$objAnimals = new Animals();
//setting properties
$objAnimals->name = "Lion";
$objAnimals->category = "Wild Animal";
//Copying object
$objCopied = $objAnimals;
$objCopied->name = "Cat";
$objCopied->category = "Pet Animal"
print_r($objAnimals);
print_r($objCopied);
?>

When we change $objCopied it affects $objAnimals. The output is,

Values of object $objAnimals:
Animals Object
(
    [name] => Cat
    [category] => Pet Animal
)
Values of Copied object $objCopied:
Animals Object
(
    [name] => Cat
    [category] => Pet Animal
)

Object Copy by clone

In the below example, we are copying objects by using PHP clone keyword. PHP’s clone method does a shallow copy and so, any changes made in the cloned object will not affect original object.

__clone is a magic method in PHP. Magic methods are predefined in PHP and starts with “__” (double underscore). They are executed in response to some events in PHP. 

<?php
//Creating instance of Animals class
$objAnimals = new Animals();
//Assigning values
$objAnimals->name = "Lion";
$objAnimals->category = "Wild Animal";
//Cloning the original object
$objCloned = clone $objAnimals;
$objCloned->name = "Elephant";
$objCloned->category = "Wild Animal";
print_r($objAnimals);
print_r($objCloned);
?>

Now we can see the difference in the output of this code.

Values of object $objAnimals:
Animals Object
(
    [name] => Lion
    [category] => Wild Animal
)
Values of Cloned object $objCopied:
Animals Object
(
    [name] => Elephant
    [category] => Wild Animal
)

Clone with Deep Copy

To perform a deep copy while cloning in PHP, then you can either overwite __clone method in the copied object’s class or serialize and unserialize the object as done below.

function deepClone($object)
{
    return unserialize(serialize($object));
}

Click Here To DOWNLOADcool

For more information of cloning in PHP, refer the php.net’s documentation on cloning.