PHP Cloning Objects – Create Object Copies with clone and __clone()

Learn how to duplicate PHP objects using the clone keyword, how to customize the cloning process with __clone(), and best practices for shallow and deep copies.


Introduction – Why Clone Objects in PHP?

In PHP, you can use the clone keyword to create a copy of an object. Unlike assigning an object to another variable (which shares the same reference), cloning creates a new instance with the same property values.

In this guide, you’ll learn:

  • How to clone objects using the clone keyword
  • How to use __clone() to customize the clone process
  • The difference between shallow and deep cloning
  • When and why to clone objects in real-world code

1. Basic Object Cloning with clone

class User {
    public $name;
}

$user1 = new User();
$user1->name = "Alice";

$user2 = clone $user1;
$user2->name = "Bob";

echo $user1->name; // Alice
echo $user2->name; // Bob

$user2 is a copy of $user1.
Changes to one object do not affect the other.


2. The __clone() Magic Method

class Account {
    public $id;
    public function __clone() {
        $this->id = uniqid(); // Reset ID when cloning
    }
}

$a1 = new Account();
$a2 = clone $a1;

echo $a1->id; // Original ID
echo $a2->id; // New unique ID

The __clone() method runs automatically after cloning.
Use it to reset, modify, or reinitialize properties in the cloned object.


3. Shallow vs Deep Cloning

Shallow Clone (Default Behavior)

class Book {
    public $author;
}

class Author {
    public $name = "John";
}

$b1 = new Book();
$b1->author = new Author();

$b2 = clone $b1;
$b2->author->name = "Sarah";

echo $b1->author->name; // Sarah 😬

Both $b1 and $b2 share the same Author object.
This is a shallow clone — nested objects are not duplicated.


Deep Clone (Manual Copy)

class Book {
    public $author;

    public function __clone() {
        $this->author = clone $this->author;
    }
}

class Author {
    public $name = "John";
}

$b1 = new Book();
$b1->author = new Author();

$b2 = clone $b1;
$b2->author->name = "Sarah";

echo $b1->author->name; // John 

In deep cloning, you clone inner objects manually inside __clone().


Best Practices

  • Use __clone() to reinitialize object state as needed
  • Deep clone nested objects that should not be shared
  • Avoid cloning if sharing is more appropriate (e.g., for stateless services)
  • Document clone behavior clearly for maintainability
  • Don’t assume clone copies everything — it’s shallow by default

Summary – Recap & Next Steps

Cloning in PHP is a powerful feature to duplicate object state, especially when the same logic needs to operate on separate data copies. Use clone and __clone() together to fine-tune how your objects behave during duplication.

Key Takeaways:

  • clone creates a new object instance with copied properties
  • Use __clone() to customize what happens during cloning
  • Deep clone inner objects manually for independence
  • Avoid shared references unless intended

Real-World Use Cases:
Prototype patterns, draft post editing, snapshot creation, form value duplication, state rollback systems


Frequently Asked Questions (FAQs)

What’s the difference between assignment and clone in PHP?
Assignment shares the same object; clone creates a new one.

Does clone copy private and protected properties?
Yes. All properties are copied, regardless of visibility.

Can I override the clone behavior?
Yes, by implementing the __clone() magic method.

Does PHP support deep cloning automatically?
No. You must manually clone nested objects.

Is clone better than serialization for duplication?
clone is faster for in-memory objects; serialization is useful for persisting and restoring state.


Share Now :
Share

📋 PHP Cloning Objects

Or Copy Link

CONTENTS
Scroll to Top