PHP clone Keyword
last modified November 25, 2026
The PHP clone keyword creates a new object with the same class and the same property values as an existing one. Assignment never copies an object; it only adds another reference to it, so clone is the way to obtain an independent instance.
This article shows how assignment and cloning differ, why the default copy is shallow, how __clone() turns it into a deep copy, how the method behaves in a hierarchy, and why readonly properties and enums need special attention.
Basic definitions
An object variable holds a handle, not the object itself. $b = $a copies the handle, so both variables point at one object, and a change through either name is visible through the other. $b = clone $a creates a second object and copies every property into it.
The copy is shallow: scalar values and arrays are copied, but an object-valued property still points at the same nested object. Arrays behave like values in PHP, so an array property is duplicated on write even without clone; the objects stored inside that array are still shared.
After copying the properties, PHP calls the __clone() method on the new object if the class declares one. The method runs on the copy, not on the original, which is why it can replace shared nested objects with their own clones. PHP does not call the parent implementation automatically.
Assignment shares the object
The first example shows the behaviour that makes clone necessary. Two variables are assigned, and the second one changes the object seen by the first.
<?php
declare(strict_types=1);
class Settings
{
public bool $debug = false;
}
$original = new Settings();
$alias = $original;
$alias->debug = true;
echo $original->debug ? "debug on\n" : "debug off\n";
echo $original === $alias ? "same object\n" : "different objects\n";
$alias is not a second settings object. It is another name for the same instance, so $alias->debug = true also turns debugging on for $original. The identity comparison === confirms it: two object variables are identical when they point at the same instance.
$ php clone_assignment.php debug on same object
clone creates a new object
Replacing the assignment with clone gives the second variable its own instance with the same starting state.
<?php
declare(strict_types=1);
class Settings
{
public bool $debug = false;
}
$original = new Settings();
$copy = clone $original;
$copy->debug = true;
echo $original->debug ? "original on\n" : "original off\n";
echo $copy->debug ? "copy on\n" : "copy off\n";
echo $original === $copy ? "same object\n" : "different objects\n";
The copy starts with debug set to false, exactly like the original. Changing the copy afterwards leaves the original untouched, and === now reports different objects. Property values were copied at the moment of cloning; later changes on either side do not travel to the other.
$ php clone_basic.php original off copy on different objects
The copy is shallow
When a property holds another object, the clone receives the same reference rather than a copy of that object. Both instances then share the nested object.
<?php
declare(strict_types=1);
class Owner
{
public function __construct(public string $name)
{
}
}
class Project
{
public Owner $owner;
public function __construct(string $owner)
{
$this->owner = new Owner($owner);
}
}
$original = new Project("Alice");
$copy = clone $original;
$copy->owner->name = "Bob";
echo "original: ", $original->owner->name, "\n";
echo "copy: ", $copy->owner->name, "\n";
Project has an Owner property. The clone gets its own Project, but $copy->owner and $original->owner point at one Owner, so renaming the owner through the copy changes the original as well. This is the default and it is often what a caller does not expect. The next section fixes it.
$ php clone_shallow.php original: Bob copy: Bob
A deep copy with __clone()
A class that owns mutable nested objects should clone them in __clone(). The method runs after the shallow copy, so each property can be replaced with its own copy.
<?php
declare(strict_types=1);
class Owner
{
public function __construct(public string $name)
{
}
}
class Project
{
public Owner $owner;
public function __construct(string $owner)
{
$this->owner = new Owner($owner);
}
public function __clone()
{
$this->owner = clone $this->owner;
}
}
$original = new Project("Alice");
$copy = clone $original;
$copy->owner->name = "Bob";
echo "original: ", $original->owner->name, "\n";
echo "copy: ", $copy->owner->name, "\n";
$this->owner = clone $this->owner; runs on the freshly created copy, so the original keeps its owner and the copy gets a separate one. The clone is now independent at both levels. A deep copy must walk every mutable object in the graph; an array of objects, for example, needs each element cloned, which a loop inside __clone() can do.
$ php clone_deep.php original: Alice copy: Bob
__clone() in a hierarchy
A child class that overrides __clone() replaces the parent method. PHP does not chain the calls, so the child must call parent::__clone() when the parent has work to do.
<?php
declare(strict_types=1);
class Node
{
public function __clone()
{
echo "Node::__clone\n";
}
}
class Doc extends Node
{
public function __clone()
{
echo "Doc::__clone\n";
}
}
class Report extends Node
{
public function __clone()
{
parent::__clone();
echo "Report::__clone\n";
}
}
$doc = new Doc();
$docCopy = clone $doc;
$report = new Report();
$reportCopy = clone $report;
Cloning a Doc prints only Doc::__clone, because its method never calls the parent. Cloning a Report prints both messages, since that method calls parent::__clone() first. The difference matters when the parent owns properties that need resetting; forgetting the call leaves those properties shared or stale. The extends keyword article covers the parent call syntax.
$ php clone_inheritance.php Doc::__clone Node::__clone Report::__clone
Readonly properties in a clone
A readonly property normally cannot be assigned twice, which would make it impossible to adjust in __clone(). Since PHP 8.3 the engine makes an exception for the clone, so a copied readonly property can be initialized again inside __clone().
<?php
declare(strict_types=1);
class Ticket
{
public function __construct(public readonly string $code)
{
}
public function __clone()
{
$this->code .= "-copy";
}
}
$original = new Ticket("T-100");
$copy = clone $original;
echo $original->code, "\n";
echo $copy->code, "\n";
Ticket gives each copy a different code by appending a suffix in __clone(). The original keeps T-100 and the copy gets T-100-copy. Before PHP 8.3 the same code raised Cannot modify readonly property, so older projects had to leave such properties out of the constructor or use a factory instead. The readonly properties article covers the modifier itself.
$ php clone_readonly.php T-100 T-100-copy
Enums cannot be cloned
An enum case is a singleton: the language guarantees one instance per case, so copying it would break that guarantee. clone on an enum raises an error.
<?php
declare(strict_types=1);
enum Suit: string
{
case Hearts = "H";
case Spades = "S";
}
try {
$copy = clone Suit::Hearts;
} catch (Error $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
The message is Trying to clone an uncloneable object of class Suit. The same error appears for any internal object that forbids cloning. Enum cases are compared with === and passed around by value already, so there is nothing to copy; the PHP enum article explains the model.
$ php clone_enum.php Error: Trying to clone an uncloneable object of class Suit
Best practices
Decide deliberately what a copy of the object means, and write that decision into __clone().
- Use
clonewhen a method must return an object that the caller may modify without affecting the receiver. - Implement
__clone()whenever the class owns mutable objects, and clone each of them explicitly. - Call
parent::__clone()from a child method so the parent's invariants survive the copy. - Prefer immutable value objects when possible; an object that never changes needs no deep copy at all.
- Document whether a clone is shallow or deep, and do not rely on callers knowing the difference.
The PHP OOP tutorial covers objects and references, and copy by value versus reference explains the same distinction for arrays and scalars.
Source
This article covered the PHP clone keyword: why assignment shares an object, how clone creates an independent instance, the shallow default, deep copies with __clone(), inheritance, readonly properties, and uncloneable enums. Continue with PHP magic methods for the full set of hooks, or read readonly properties for immutable objects.
Author
List all PHP tutorials.