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.
Following is output of above code:
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.
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 :
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 exampleclass 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.
No comments:
Post a Comment