What it is and why to use it
Assigning one object variable to another does not create a separate object. Both variables refer to the same instance. The clone keyword creates a different instance with a shallow copy of its properties. This is useful for editable drafts, but a shallow copy does not automatically duplicate every object reachable through those properties.
How it works
After copying the outer object’s properties, PHP calls the new object’s __clone() method when one is defined and accessible. That method can replace selected nested references with clones. Ordinary nested object properties otherwise continue to refer to the same nested instance. Reference-valued properties can also remain shared; there is no universal recursive deep-copy policy built into clone.
Common mistakes and limitations
Do not assume $copy = $original gives an independent draft, or that clone safely duplicates open files, database connections and arbitrary object graphs. Decide which nested values should be shared and which should be copied. Cycles and resource ownership make generic deep-copy helpers much more complex than this example.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$copy = clone $original;Examples
Compare assignment with an independent outer copy
Observe which variables point to the same note.
<?php
declare(strict_types=1);
class Note {
public function __construct(public string $title) {}
}
$original = new Note('Draft');
$alias = $original;
$copy = clone $original;
$alias->title = 'Shared change';
$copy->title = 'Independent copy';
echo $original->title, PHP_EOL;
echo $copy->title, PHP_EOL;
- alias and original refer to the same instance, so changing alias also changes what original sees.
- clone creates a separate Note instance for copy.
- The string properties can now be assigned independently. This supports a simple draft model with scalar properties.
- Expected output: Shared change Independent copy
Copy one owned nested object explicitly
Use __clone to prevent a nested address edit from changing the original.
<?php
declare(strict_types=1);
class Address {
public function __construct(public string $city) {}
}
class Profile {
public function __construct(public Address $address) {}
public function __clone(): void {
$this->address = clone $this->address;
}
}
$original = new Profile(new Address('Leeds'));
$draft = clone $original;
$draft->address->city = 'York';
echo $original->address->city, PHP_EOL;
echo $draft->address->city, PHP_EOL;
- The outer clone initially copies the reference to the Address object.
- __clone then replaces that reference in the new Profile with a separate Address clone.
- Changing the draft’s city leaves the original at Leeds. This is an explicit one-level ownership decision, not an automatic deep copy of every possible property.
- Expected output: Leeds York
Best practices
- Define copy behaviour around the object’s actual ownership rules.
- Test nested mutation as well as top-level property changes.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual