Create a copy of an object in PHP
This article demonstrates how to create a copy of an object in PHP.
1. Using clone operator
In PHP, objects are passed by reference. To make a shallow copy of the object, you can clone it using the clone operator. The clone operator performs a shallow copy of all an object’s properties, but any references to other objects will remain references. In shallow copy, if the property is of scalar type, the value is copied; if the property value is a reference to another object, the reference is copied, hence referring to the same instance. If either of these objects is changed, the change is reflected in the others.
You can use the $object_copy = clone $object; syntax to create a shallow copy of an object. The following example demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 |
<?php $point = new StdClass; $point->x = 0; $point->y = 0; $point->z = 0; $copy = clone($point); print_r($copy); ?> |
Output:
stdClass Object
(
[x] => 0
[y] => 0
[z] => 0
)
2. Using __clone() function
If your object holds a reference to another object, you can define the __clone() function to additionally call the clone operator on each of the objects. If done correctly, this will result in a deep copy of an object. The following example, taken from the PHP Object Cloning documentation, demonstrates the use of the __clone() function to facilitate necessary replication of the parent object’s properties with the clone operator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php class SubObject { static $instances = 0; public $instance; public function __construct() { $this->instance = ++self::$instances; } public function __clone() { $this->instance = ++self::$instances; } } class MyCloneable { public $object1; public $object2; function __clone() { // Force a copy of this->object, otherwise // it will point to same object. $this->object1 = clone $this->object1; } } $obj = new MyCloneable(); $obj->object1 = new SubObject(); $obj->object2 = new SubObject(); $obj2 = clone $obj; print("Original Object:\n"); print_r($obj); print("Cloned Object:\n"); print_r($obj2); ?> |
Output:
Original Object:
MyCloneable Object
(
[object1] => SubObject Object
(
[instance] => 1
)
[object2] => SubObject Object
(
[instance] => 2
)
)
Cloned Object:
MyCloneable Object
(
[object1] => SubObject Object
(
[instance] => 3
)
[object2] => SubObject Object
(
[instance] => 2
)
)
Note that the object’s __clone() function is invoked only if it is defined. Unlike other programming languages, __clone() method does not override the default cloning process. It will be invoked after cloning is done by the clone operator to allow any further changes to the copied properties.
That’s all about creating a copy of an object in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)