Wednesday, 12 September 2018

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 ...

Related posts:

0 comments:

Post a Comment