20 C
Jaipur
Thursday, November 26, 2020

How to clone an object in PHP – Linux Hint

Must read

Redmi Note 9 Pro 5G, Redmi Note 9 5G, & Redmi Note 9 4G Launched Starting at CNY 999

Though the Redmi Note 9 series has been on sale in India for close to six months, it is today that the company has...

Redmi Watch with 1.4-inch Color Display, 7-Day Battery Life Launched in China

As teased earlier this week, Xiaomi has unveiled the Redmi Watch alongside the new Redmi Note 9 5G series in China. This is the...

Add, Delete And Grant Sudo Privileges To Users In CentOS

A "sudo" user can run an administrative task or command which a normal user is not allowed to. This guide explains how to add,...

Nokia Smart TVs With up to a 75″ 4K Display Launched Starting at €400

Following the launch of the Nokia Streaming Box 8000 earlier this month, StreamView GmbH, the official licensee of Nokia Android TVs, set top boxes...

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Redmi Note 9 Pro 5G, Redmi Note 9 5G, & Redmi Note 9 4G Launched Starting at CNY 999

Though the Redmi Note 9 series has been on sale in India for close to six months, it is today that the company has...

Redmi Watch with 1.4-inch Color Display, 7-Day Battery Life Launched in China

As teased earlier this week, Xiaomi has unveiled the Redmi Watch alongside the new Redmi Note 9 5G series in China. This is the...

Add, Delete And Grant Sudo Privileges To Users In CentOS

A "sudo" user can run an administrative task or command which a normal user is not allowed to. This guide explains how to add,...

Nokia Smart TVs With up to a 75″ 4K Display Launched Starting at €400

Following the launch of the Nokia Streaming Box 8000 earlier this month, StreamView GmbH, the official licensee of Nokia Android TVs, set top boxes...

Here’s One Solid Reason To Buy 2020 Mahindra Thar: Safest 4WD Indian SUV

2020 Mahindra Thar has recently undergone a Global NCAP #SaferCarsForIndia crash test and secured four stars rating in it. Interestingly, Mahindra Thar scored four...