PHP Object Cloning: clone Keyword, Syntax & Examples

1. Introduction

In Object-Oriented Programming (OOP), objects are created from classes and are used to represent real-world entities in a program. Sometimes developers need to create an exact copy of an existing object. This process is known as Object Cloning in PHP.

OR

Object Cloning is the act of creating a duplicate copy of an object. The new object will contain the same properties and values as the original object.

Note: In PHP, objects are assigned by reference by default, not by value. This means that when you copy an object using the assignment operator (=), both variables will point to the same object in memory. If one object changes, the other will also reflect the changes.

📖
Important Notes:
  • If you change the value of the original object, the copied object will also be affected.
  • If you change the copied object, the original object will also change.
  • To avoid this problem and create an independent object, PHP provides Object Cloning using the clone keyword.

2. Simple Object Copy (By Reference)

When an object is copied directly in PHP using the assignment operator (=), it does not create a new object. Instead, it creates another reference to the same object.

Example


class Student {
   public $stu_name = "John";
}
$stu_obj1 = new Student;
$stu_obj2 = $stu_obj1;

$stu_obj2->stu_name = "Tony";

echo $stu_obj1->stu_name;
echo "
"; echo $stu_obj2->stu_name;

Output

Tony
Tony

Explanation

In this example:

  1. We created a class called Student with a property stu_name.
  2. Then we created an object $stu_obj1.
  3. After that, we copied the object using:

$stu_obj2 = $stu_obj1;

PHP does not create a new object. Instead, both $stu_obj1 and $stu_obj2 point to the same object in memory.

When we change:


$stu_obj2->stu_name = "Tony";

the value is changed in the original object as well.

So when we print both objects, they display the same result.

This method is useful when you want multiple references to the same object, but it becomes a problem when you need separate independent objects.

3. Object Copy Using Clone Keyword (By Value)

To create a separate copy of an object, PHP provides the clone keyword.
The clone keyword creates a new object with the same properties and values as the original object.

Example


class Student {
   public $stu_name = "John";
}
$stu_obj1 = new Student;
$stu_obj2 = clone $stu_obj1;
$stu_obj2->stu_name = "Tony";
echo $stu_obj1->stu_name;
echo "
"; echo $stu_obj2->stu_name;

Output

John
Tony

Explanation

In this example:
1. We created a class Student with the property stu_name.
2. Then we created an object $stu_obj1.
3. Instead of assigning the object directly, we used:


$stu_obj2 = clone $stu_obj1;

This creates a completely new object.

After cloning the object, we changed:


$stu_obj2->stu_name = "Tony";
  • $stu_obj1 still contains John
  • $stu_obj2 contains Tony

This shows that cloning creates an independent object.

4. Object Cloning with Magic Method __clone()

PHP also provides a magic method called __clone(). This method is automatically executed when an object is cloned using the clone keyword.
The __clone() method allows developers to customize the cloning process.
It is useful when you want to modify or reset some properties during cloning.

Example


class Student {
   public $stu_name = "John";
   function __clone() {
       $this->stu_name = "Tony";
   }
}
$stu_obj1 = new Student;
$stu_obj2 = clone $stu_obj1;
echo $stu_obj1->stu_name;
echo "
"; echo $stu_obj2->stu_name;

In this example:
1. We created a class Student with property stu_name.
2. Inside the class, we defined a magic method __clone().
3. When the object cloning happens:


$stu_obj2 = clone $stu_obj1;

the __clone() method is automatically executed.
Inside this method, we wrote:


$this->stu_name = "Tony";

This means that when the object is cloned, the property value is changed to Tony.
So:

  1. Original object remains John
  2. Cloned object becomes Tony

This shows how the __clone() method controls cloning behavior.

5. Real-Life Example of Object Cloning

Consider a User Profile System where a template user object is used to create multiple user objects.

Instead of creating each user object from scratch, we can clone the template object.

Example


class User {
   public $role = "User";
   public $status = "Active";
}
$templateUser = new User();
$newUser = clone $templateUser;
$newUser->role = "Admin";
echo "Template Role: " . $templateUser->role;
echo "
"; echo "New User Role: " . $newUser->role;

Output

Template Role: User
New User Role: Admin

Explanation

In this example:

  • $templateUser acts as a base object
  • $newUser is created by cloning the template

We modified the role of $newUser without affecting $templateUser.

This approach is commonly used in:

  • User management systems
  • Template-based systems
  • Game development
  • Object duplication systems

6. Advantages of Object Cloning

Object cloning provides several benefits:

1. Independent Objects

Cloned objects are independent and do not affect the original object.

2. Faster Object Creation

Cloning can be faster than creating new objects manually.

3. Object Duplication

Useful when multiple objects share similar properties.

4. Custom Cloning

The __clone() method allows customization during cloning.

7. Conclusion

Object Cloning is an important feature in PHP Object-Oriented Programming. It allows developers to create exact copies of objects without affecting the original object.

By default, PHP copies objects by reference, which means multiple variables can refer to the same object. However, using the clone keyword, developers can create a separate copy of an object.

Additionally, the __clone() magic method provides the flexibility to customize the cloning process.

PHP Object Cloning – Interview Questions

Q 1: What is object cloning in PHP?
Ans: Creating a copy of an existing object.
Q 2: Which keyword is used to clone an object?
Ans: clone.
Q 3: What is __clone() method?
Ans: It is called automatically during object cloning.
Q 4: Does cloning create a deep copy?
Ans: No, it creates a shallow copy by default.
Q 5: Why is object cloning useful?
Ans: To avoid modifying the original object.

PHP Object Cloning – Objective Questions (MCQs)

Q1. Which keyword is used to clone an object in PHP?






Q2. Cloning creates ______.






Q3. Which magic method is called when an object is cloned?






Q4. Can cloned objects have different property values from the original?






Q5. Cloning is used to ______.






Related PHP Tutorials