PHP Object Cloning

by Vincy. Last modified on July 6th, 2023.

Object cloning is creating a copy of an object. An object copy is created using the clone keyword, and the __clone() method cannot be called directly. In PHP, cloning an object is doing a shallow copy, not a deep one.

You mean that the contained objects of the copied objects are not copied. If you wish for a deep copy, you must 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.

Copy Objects by Assignment

In the code below, I am attempting to copy an object using the assignment operator. So what happens is that 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 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 example below, we are copying objects using the PHP clone keyword. PHP’s clone method does a shallow copy, so any changes made in the cloned object will not affect the original object.

__clone is a magic method in PHP. Magic methods are predefined in PHP and start 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
)

download

Clone with Deep Copy

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

<?php

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

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

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Object Cloning”

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page