One cannot create a copy constructor in PHP as overloading of
methods is not allowed. But to get a full replica of an object, we can use a keyword clone
provided by PHP.
Syntax
$obj1 = clone $obj2;
Note: Cloning an object will create a shallow copy of it, and any property that is a reference will remain a reference in the copied object as well.
The method __clone
is called after cloning the object with the keyword clone
, and further changes can be achieved by defining the __clone
method in the
class.
Example
How to use keyword `clone` in PHP
Explanation
- Lines 2–5: An example class
Foo
is created. - Line 7: We create a new object of
Foo
class. - Line 8: We show to create a shallow copy of an object using
clone
. - Line 9: We update the value of
foo_resource
of$a
object. - The output from line 19 confirms that a separate copy was created and it remained intact after the original one was changed.
Example of __clone[]
Now let’s see how to add the __clone[]
method to achieve custom functionality with the keyword clone
.
Changing properties after cloning
Explanation
Lines 2–11: We define an example class
Foo
.Line 8: We reference the
foo_ref_resource
property in the__clone[]
method to create a copy of the referenced resource.Line 13: We create a string variable.
Line 14: We create a new object of
Foo
class.Line 15: We update the reference address in the
foo_recource
of$a
object.After creating a copy of the provided object on line 16, the
__clone[]
method will be called.Line 17: We assign a new value to the
$ref_string
.Note: From lines 18 and 19, the output of the original string is changed because it is being accessed by reference while the cloned copy is not changed.
Conclusion
You can use the keyword clone
to create a shallow copy of objects and implement __clone[]
in your class to further customize the copied properties.
Caution: Calling
clone
on a non-object variable will result in a fatal error.
CONTRIBUTOR
Copyright ©2022 Educative, Inc. All rights reserved
Home / Copying an object in PHP with clone
Objects in PHP are assigned by reference, so copying an object with the = operator will result in a second variable referencing the same object and not an actual copy. Enter PHP’s clone function which creates a shallow copy of the object.
Using = to copy an object copies the reference only
To illustrate how PHP only makes a reference to the original object using the = operator look at the following example. [Note that I’ve made the = assignment red in the example].
class foo {
public $bar;
}
$foo = new foo[];
$foo->bar = 'baz';
$foo2 = $foo;
$foo2->bar = 'bat';
echo $foo->bar;
echo $foo2->bar;
Both echo lines at the end of the example will echo "bat" and not "baz" and "bat", because $foo2 is a reference to $foo and accessing properties and calling methods for $foo2 is actually referring to those properties and methods of $foo.
Using clone to copy and object copies the object
Using the same example, but using clone instead to copy the object will result in a copy of the original object. Changing properties and calling methods will do so to the copied object and not the original.
The only difference between the above example and this one is "=" becomes "clone" which is highlighted in red in both examples.
class foo {
public $bar;
}
$foo = new foo[];
$foo->bar = 'baz';
$foo2 = clone $foo;
$foo2->bar = 'bat';
echo $foo->bar . "n";
echo $foo2->bar . "n";
The second example outputs "baz" and then "bat".
Shallow copy
Cloning an object creates a "shallow" copy of the original object. What this means is that any properties that are references to other variables will remain references to those variables and not copies themselves.
The __clone method
If the object contains a __clone method it will be called when the object is cloned. It must be a publicly accessible function and cannot be called directly otherwise PHP will end with a fatal error.You wouldn’t actualy echo output when cloning, but if the object was cloned in the example below, it would echo out "i’ve been cloned".
class foo { public $bar; public function __clone[] { echo "i've been cloned"; } }
If you attempt to call the __clone[] method yourself, the following error will occur:
Fatal error: Cannot call __clone[] method on objects - use 'clone $obj' instead in ...