Tutorials

PHP Magic Method __clone() tutorial

Posted by I. B. Gd Pramana A. Putra, 13 Mar 22, last updated 18 Jul 22

In this lesson, we’re going to learn and get familiar with PHP __clone() Magic Method tutorial along with example and explanation.

PHP Magic Method: __clone Definition

__clone in the PHP programming language is one of the magic methods that will be called/executed when an object is being cloned (duplicated) with clone statement.

PHP Magic Method: __clone Declaration

In this part, we will learn how to implement the __clone magic method and the uses case of the __clone.

The __clone magic method is declared with the following format:

__clone ( void ) : void

It takes not even a single argument as its parameter, this doesn’t return a value.

PHP Magic Method: __clone Example

Object cloning in PHP programming language is done by using clone reserved keyword, and the __clone magic method has defined in that object.

The purpose of object cloning is to copy/duplicate the object reference, including all the attributes that the object has, such as properties and methods without interfering with the original object.

Let’s take a look at the example. We have a Car class with public attribute $name.

class Car
{
    public $name;
}

With our Car class, we will instantiate a new object, and later duplicate the instantiated object without using the clone keyword.

$car = new Car();
$car->name = 'Range Rover Evoque'; 

$car2 = $car;
$car2->name = 'Honda HRV';

Please notice that we are assigning the $car object to the $car2 object.

Let’s print out those two objects, and see what the PHP interpreter will show.

echo $car->name . '<br/>';
echo $car2->name . '<br/>';

The result of printing out those two objects is:

Both of the objects returned us the same value. But notice that on the code example above, we’ve assigned the first object with “Range Rover Evoque”, and the second object with “Honda HRV”.

The question is why the outputs from those two objects are the same? Why the outputs are not “Range Rover Evoque” and “Honda HRV”?

Well, that’s because those two objects are still referencing the same memory, even though we’ve duplicated them.

On our code example, we’ve duplicated the object $car with $car2. However, the attribute value of $name from the $car object changes as it’s referencing to the same address (memory reference).

So, in order to duplicate or clone an object in PHP without using or referencing the same memory reference, the clone keyword was built for this purpose.

Now let’s try the following code, with the clone keyword.

$car = new Car();
$car->name = 'Range Rover Evoque'; 

$car2 = clone $car;
$car2->name = 'Honda HRV';

Let’s echo our objects again.

echo $car->name . '<br/>';
echo $car2->name . '<br/>';

The PHP interpreter now shows us that those two objects are no longer using the same memory reference.

The conclusion is that the clone keyword is coming handy to clone an object without copying the same memory reference from the original object.

Anyway, when the object is duplicated or cloned with the clone keyword, it’s also executing the __clone() magic method if possible. However, the __clone() can not be called directly without the object instantiation.

Reference: PHP Magic Method __clone – PHP.net

Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.