Object Cloning in PHP

If you will directly copy objects in php, then it will copy by reference, not by value. Means if you will change main object data then copied object will be affected. Also if you will change value of the copied object then main object value will be changed. So if you want to create copy of the object which should never referenced to original object then you can take help of object cloning in php. In this tutorial we will explore what is usual object copy by reference and then object cloning. Also in end of this tutorial we will use __clone magic method to customize  object cloning process in php.

Object copy or by reference copy

Typical copy of object in php works by reference. Means both(main and copied) object will be interlinked. For example
class test
{
public $a;
private $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
}
$a = new test("ankur" , "techflirt");
$b = $a; //Copy of the object
$a->a = "no Ankur";
print_r($a);
print_r($b);

Following is output of above code:

test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)

So here in object $a after copy it to $b, we have changed its object value a to “no Ankur”, and then we have print both object value and found that $b has changes made in $a. This is one of the very useful when you want object by reference. But when you need object copy by value this is show stoper. So to overcome with this limitation php has provided feature of object cloning separably.

Implementation of Object Cloning in PHP

As we have seen that typical copy of object in php is by reference. We can copy two object by value in php by value using method of object cloning. Object cloning in php can be implemented using clone keyword. Following is example of object cloning:

class test
{
public $a;
private $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
}
$a = new test("ankur" , "techflirt");
$b = $a; //Copy of the object
$c = clone $a; //clone of the object
$a->a = "no Ankur";
print_r($a);
print_r($b);
print_r($c);

In above example $c is clone of $a variable. So $c is completely separate from object $a and $b. Following is output of above code.

test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)

Object cloning with magic method __clone

Suppose you want to change value of your property a of the test class in case of cloning of object in php. We can change behavour of the clone object in php using magic method __clone. Magic method clone executes when object cloning is performed. As soon as php execute statement $c = clone $a, __clone method invoked. Following is example of the object cloning and __clone magic method in php

<?php
class test
{
public $a;
private $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
function __clone()
{
$this->a = "c";
}
}
$a = new test("ankur" , "techflirt");
$b = $a; //Copy of the object
$c = clone $a; //clone of the object
$a->a = "no Ankur";
print_r($a);
print_r($b);
print_r($c);
print_r($a);

So here $c->a will be “c”. Because __clone method will be invoked and only for object $c it will set value of $this->a to ‘c’. Following is outupt of above example :

test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => ankur
    [b:test:private] => techflirt
)
test Object
(
    [a] => no Ankur
    [b:test:private] => techflirt
)

Object cloning in php has various feature and flaxibity to clone your object. Hope you have clear understanding of object cloning in php.

For more details about object cloning in php you can read:
http://php.net/manual/en/language.oop5.cloning.php
http://en.wikipedia.org/wiki/Object_copy

Next >> Method Chaining in PHP

Comments

  1. jaroslav says

    Hi Ankur,
    is the output of print_r($c) correct? Shouldn’t be

    test Object
    (
    [a] => ankur
    [b:test:private] => c
    )

  2. custom php development says

    Generally I do not comment on any blog post but yours is very eye catching and informative so I have leave one.

  3. Rahul says

    Thank you for the post. I think there is an error in the last example, I mean the output for print_r($c);

  4. Abachem says

    Please just say the correct answer is:

    print_r;

    (output)

    test Object
    (
    [a] => c
    [b:test:private] => techflirt
    )

    for we beginners who are just trying to get our heads around PHP. This is a beginner’s tutorial after all.
    TY to Ankur for creating this short lesson — it helped me better grasp the PHP Manual.

Leave a Reply

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


− eight = zero

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>