Clone objects
In PHP an object variable holds a handle, and that one fact decides the whole lesson: a plain assignment shares the object, and only clone builds a second one.
clone keyword
Assignment copies the handle, not the object. Both variables then point at the same data in memory.
The clone keyword builds a real copy with its own property values. Later edits to the copy leave the source alone.
<?php $order = new stdClass(); $order->total = 10; $alias = $order; $alias->total = 11; echo $order->total, PHP_EOL; $copy = clone $order; $copy->total = 20; echo $order->total, PHP_EOL; echo $copy->total, PHP_EOL;
php clone.php
11
11
20
The alias changed the original, because it was the original. The clone did not: its 20 left the order at 11.
Customize with __clone
The __clone method runs on the new copy right after cloning. It is the place to reset fields that must stay unique.
Counters, handles, and timestamps are typical candidates. Give the copy fresh values instead of sharing the old ones.
<?php class Cart { public int $id = 0; public function __clone(): void { $this->id = 0; } } $cart = new Cart(); $cart->id = 5; $copy = clone $cart; echo $cart->id, PHP_EOL; echo $copy->id, PHP_EOL;
php custom.php
5
0
The source keeps its id and the copy starts from a fresh one, because __clone() ran on the copy only.
Deep copies
Clone copies nested objects by handle, so the copy still points at the same inner object. Edits through one copy would leak into the other.
Clone the inner objects inside __clone to cut that link. Each level of nesting needs its own clone call.
<?php class Order { public stdClass $box; public function __construct() { $this->box = new stdClass(); } public function __clone(): void { $this->box = clone $this->box; } } class Shallow { public stdClass $box; public function __construct() { $this->box = new stdClass(); } } $a = new Order(); $a->box->n = 1; $b = clone $a; $b->box->n = 2; echo $a->box->n, PHP_EOL; $c = new Shallow(); $c->box->n = 1; $d = clone $c; $d->box->n = 2; echo $c->box->n, PHP_EOL;
php deep.php
1
2
The order with the inner clone kept its own 1 and the copy took 2. The class without __clone() shares its box, so the write through the copy reached the source and both ended at 2.
Compare with ==
The == operator reports a match when class and property values match. Different instances with equal data count as equal.
The === operator is stricter and asks for the same instance. Use it when identity matters more than shape.
<?php $a = new stdClass(); $b = new stdClass(); $a->x = 1; $b->x = 1; var_dump($a == $b); var_dump($a === $b);
php compare.php
bool(true)
bool(false)
Two different objects with the same data are loosely equal and strictly different, which is exactly the difference between shape and identity.
Exercises
- Assign an object to a second variable, change a property through the alias and print both.
- Give a class a __clone() that clears a unique id and show the result.
- Clone an object that holds an array and find out whether the array is shared.
- Turn the shallow class of the deep section into a deep copy and prove the difference.
- Compare three objects with == and === and explain each result.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
$b = $a;copy when$aholds an object?Answer
The handle, so both names point at one object
Objects are handled by reference-like handles, so a plain assignment shares the instance.
-
When does
__clone()run?Answer
On the new copy, right after it is cloned
The method is called on the copy so it can give fresh values to fields that must stay unique.
-
Why does a clone still share a nested object?
Answer
Because clone copies properties shallowly, and an object property is a handle
The property value is the handle of the inner object, and the copy of a handle points at the same object.
-
What does
$a == $bcheck for two objects?Answer
That they have the same class and equal property values
Loose comparison of objects compares class and properties; identity is the job of ===.
-
What does this program print?
<?php $order = new stdClass(); $order->total = 10; $alias = $order; $alias->total = 11; echo $order->total;Answer
11
The alias and the original are the same object, so the write is visible through both names.
-
What does this program print?
<?php $order = new stdClass(); $order->total = 10; $copy = clone $order; $copy->total = 20; echo $order->total, " ", $copy->total;Answer
10 20
The clone has its own property slot, so the second write never reaches the original.
-
What does this program print?
<?php class Cart { public int $id = 0; public function __clone(): void { $this->id = 0; } } $cart = new Cart(); $cart->id = 5; $copy = clone $cart; echo $cart->id, " ", $copy->id;Answer
5 0
__clone() ran on the copy only, so the source kept 5 and the copy restarted at 0.
-
What does this program print?
<?php class Shallow { public stdClass $box; public function __construct() { $this->box = new stdClass(); } } $c = new Shallow(); $c->box->n = 1; $d = clone $c; $d->box->n = 2; echo $c->box->n;Answer
2
Without a clone of the inner object both copies share one box, so the write is visible through both.
-
What does this program print?
<?php $a = new stdClass(); $b = new stdClass(); $a->x = 1; $b->x = 1; var_dump($a === $b);Answer
bool(false)
Two separate instances are never identical, even with equal data.
-
How do you make a deep copy of an object that holds another object?
Answer
Clone the inner object inside
__clone()Each level of nesting needs its own clone call, and __clone() is where the outer copy makes it.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev