php

How to clone an object in PHP

In an object-oriented programming language, an object acts as a reference variable. Therefore, if an object variable is assigned to another variable using the assignment operator, then both variables will reference the same location, and if the value of one variable is changed, then the value of the other variable will change automatically.

In this tutorial, we will clone a PHP object to create a copy of the object.

Using the assignment operator to copy an object

The value of the object variable can be copied to another variable in PHP by using object cloning. In PHP, a shallow copy of the object can be created by using the keyword clone, and a deep copy of the object can be created by using the method __clone(). In the following example, we use the keyword clone and the method __clone() when an object variable is assigned to another variable.

Use the code below to create a PHP file called withoutclone.php, and store the file in the webserver location. Here, a class named Course is declared; Course contains a variable, $course_id, and a function, Details(). The function will print the course details based on the course ID. Next, an object named $objcourse is declared to access the class members.

Once the value of $course_id is assigned using this object and the object variable is assigned to $objcrs, both variables will use the same location in the memory for the reference. Therefore, if the value of one object is changed, then the other object will be automatically modified if they are copied.

<?php
//Define a class
Class Course
{
    //Declare variable
    public $course_id;
    //Declare method
    public function Details()
    {
        //Print the output based on the condition
        if($this->course_id == 'CSE-202')
        {
            echo "Course Name: Data structure<br />";
            echo "Credit Hour:2.0<br />";
        }
        else if($this->course_id == 'CSE-305')
        {
            echo "Course Name: Object Oriented Programming";
            echo "<br />Credit Hour:3.0";
        }
    }
}
//Create object of Course class
$objcourse = new Course();
//set property
$objcourse->course_id = "CSE-202";

echo "<b>The output before assigining new value:</b><br /><br />";
//Call the method
$objcourse->Details();
//Copying object
$objcrs = $objcourse;
//Set new value for the copied object
$objcrs->course_id = "CSE-305";

echo "<br /><b>The output after assigining new value:</b><br /><br />";
//Again call the method
$objcourse->Details();
?>

You will get the following output after running the script from the webserver. Here, if the value of $objcrs is changed, then the value of $objcourse is changed to the same value.

Using the clone keyword to copy an object

The clone keyword can be used to solve the above problem. Use the following code to create a file named clone.php. The same class is used here. The object of the class is cloned to another object by using the clone keyword to create a copy of the object. If the value of one variable changes, then it will not change the value of the other variable.

<?php
//Define a class
Class Course
{
    //Declare variable
    public $course_id;
    //Declare method
    public function Details()
    {
        //Print the output based on the condition
        if($this->course_id == 'CSE-202')
        {
            echo "Course Name: Data structure<br />";
            echo "Credit Hour:2.0<br />";
        }
        else if($this->course_id == 'CSE-305')
        {
            echo "Course Name: Object Oriented Programming<br />";
            echo "Credit Hour:3.0";
        }
    }
}
//Create object of Course class
$objcourse = new Course();
//set property
$objcourse->course_id = "CSE-202";
echo "<b>The output of the main object before clonning and assiging new value:</b><br /><br />";

//Call the method
$objcourse->Details();

//Clone object
$objcrs = clone $objcourse;
//Set new value for the copied object
$objcrs->course_id = "CSE-305";
echo "<br /><b>The output of the main object after clonning and assigining new value:</b><br /><br />";
//Call the method for $objcourse
$objcourse->Details();
echo "<br /><b>The output of the clonned object:</b><br /><br />";
//Call the method for $objcrs
$objcrs->Details();
?>

You will get the following output after running the script from the webserver. Here, when the value of $objcrs is changed, the value of $objcourse will remain unchanged.

Using the __clone() method to copy an object

The __clone() method can also be used to solve the above problem. Using the code bwlow, create a file named withclonemethod.php. The same class is used here. The __clone() method is used in the script with the clone keyword. This method is called automatically when an object is cloned to another object. According to the script, a new value is assigned for the cloned object inside the __clone() method.

<?php
//Define a class
Class Course
{
    //Declare variable
    public $course_id;
    //Define __clone() method
    public function __clone() {
        $this->course_id = "CSE-305";
    }

    //Declare method
    public function Details()
    {
        //Print the output based on the condition
        if($this->course_id == 'CSE-202')
        {
            echo "Course Name: Data structure<br />";
            echo "Credit Hour:2.0<br />";
        }
        else if($this->course_id == 'CSE-305')
        {
            echo "Course Name: Object Oriented Programming";
            echo "<br />Credit Hour:3.0";
        }
    }
}
//Create object of Course class
$objcourse = new Course();
//set property
$objcourse->course_id = "CSE-202";
echo "<b>The output of the main object before clonning and assiging new value:</b><br /><br />";

//Call the method
$objcourse->Details();

//Clone object
$objcrs = clone $objcourse;
echo "<br /><b>The output of the main object after clonning:</b><br /><br />";
//Call the method for $objcourse
$objcourse->Details();
echo "<br /><b>The output of the clonned object:</b><br /><br />";
//Call the method for $objcrs
$objcrs->Details();

?>

You will get the following output after running the script from the webserver. Here, when $objcrs is cloned, its value is changed by the __clone() method, but the value of $objcourse remains unchanged. The value of $objcourse is printed before and after cloning. The value of $objcrs is printed later.

Conclusion

Cloning is a useful feature of PHP that can be used to create a copy of an object. Both the clone keyword and the __clone() method are shown in this tutorial via simple examples. We hope this tutorial helped you understand object cloning.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.