Copying an object in PHP with clone
Posted November 10th, 2009 in PHP
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 ...
Related posts:
- Using the is_callable function in PHP (Thursday, January 22nd 2009)
- How to check if a class method exists in PHP (Friday, January 16th 2009)
- Check if a class exists with PHP (Thursday, December 11th 2008)
- Method chaining with PHP (Sunday, December 7th 2008)
- Get the class name and parent class name in PHP (Wednesday, November 19th 2008)
- Get a list of all available classes with PHP (Thursday, July 10th 2008)
Subscribe / Follow / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email to have my posts sent in a daily email, follow me on Twitter or follow me on Facebook.
At least one new post is usually made every day. See my posting schedule for more details.
