November 28, 2020
How to clone an object in PHP – Linux Hint

In an object-oriented programming language, an object acts as a reference variable. Subsequently, if an object variable is assigned to a different variable utilizing the project operator, then each variables will reference the identical location, and if the worth of 1 variable is modified, then the worth of the opposite variable will change mechanically.

On this tutorial, we’ll clone a PHP object to create a replica of the thing.

Utilizing the project operator to repeat an object

The worth of the thing variable will be copied to a different variable in PHP by utilizing object cloning. In PHP, a shallow copy of the thing will be created by utilizing the key phrase clone, and a deep copy of the thing will be created by utilizing the tactic __clone(). Within the following instance, we use the key phrase clone and the tactic __clone() when an object variable is assigned to a different variable.

Use the code beneath to create a PHP file known as withoutclone.php, and retailer the file within the webserver location. Right here, a category named Course is said; Course incorporates a variable, $course_id, and a perform, Particulars(). The perform will print the course particulars based mostly on the course ID. Subsequent, an object named $objcourse is said to entry the category members.

As soon as the worth of $course_id is assigned utilizing this object and the thing variable is assigned to $objcrs, each variables will use the identical location within the reminiscence for the reference. Subsequently, if the worth of 1 object is modified, then the opposite object can be mechanically modified if they’re copied.

//Outline a category
Class Course
{
//Declare variable
public $course_id;
//Declare technique
public perform Particulars()
{
//Print the output based mostly on the situation
if($this->course_id == ‘CSE-202’)

else if($this->course_id == ‘CSE-305’)

}
}
//Create object of Course class
$objcourse = new Course();
//set property
$objcourse->course_id = “CSE-202”;

echo “The output earlier than assigining new worth:

“;
//Name the tactic
$objcourse->Particulars();
//Copying object
$objcrs = $objcourse;
//Set new worth for the copied object
$objcrs->course_id = “CSE-305″;

echo ”
The output after assigining new worth:

“;
//Once more name the tactic
$objcourse->Particulars();
?>

You’ll get the next output after operating the script from the webserver. Right here, if the worth of $objcrs is modified, then the worth of $objcourse is modified to the identical worth.

How to clone an object in PHP – Linux Hint

Utilizing the clone key phrase to repeat an object

The clone key phrase can be utilized to unravel the above downside. Use the next code to create a file named clone.php. The identical class is used right here. The item of the category is cloned to a different object by utilizing the clone key phrase to create a replica of the thing. If the worth of 1 variable adjustments, then it is not going to change the worth of the opposite variable.

//Outline a category
Class Course
{
//Declare variable
public $course_id;
//Declare technique
public perform Particulars()
{
//Print the output based mostly on the situation
if($this->course_id == ‘CSE-202’)

else if($this->course_id == ‘CSE-305’)

}
}
//Create object of Course class
$objcourse = new Course();
//set property
$objcourse->course_id = “CSE-202”;
echo “The output of the primary object earlier than clonning and assiging new worth:

“;

//Name the tactic
$objcourse->Particulars();

//Clone object
$objcrs = clone $objcourse;
//Set new worth for the copied object
$objcrs->course_id = “CSE-305″;
echo ”
The output of the primary object after clonning and assigining new worth:

“;
//Name the tactic for $objcourse
$objcourse->Particulars();
echo ”
The output of the clonned object:

“;
//Name the tactic for $objcrs
$objcrs->Particulars();
?>

You’ll get the next output after operating the script from the webserver. Right here, when the worth of $objcrs is modified, the worth of $objcourse will stay unchanged.

How to clone an object in PHP – Linux Hint

Utilizing the __clone() technique to repeat an object

The __clone() technique will also be used to unravel the above downside. Utilizing the code bwlow, create a file named withclonemethod.php. The identical class is used right here. The __clone() technique is used within the script with the clone key phrase. This technique is known as mechanically when an object is cloned to a different object. In response to the script, a brand new worth is assigned for the cloned object contained in the __clone() technique.

//Outline a category
Class Course
{
//Declare variable
public $course_id;
//Outline __clone() technique
public perform __clone() {
$this->course_id = “CSE-305”;
}

//Declare technique
public perform Particulars()
{
//Print the output based mostly on the situation
if($this->course_id == ‘CSE-202’)

else if($this->course_id == ‘CSE-305’)

}
}
//Create object of Course class
$objcourse = new Course();
//set property
$objcourse->course_id = “CSE-202”;
echo “The output of the primary object earlier than clonning and assiging new worth:

“;

//Name the tactic
$objcourse->Particulars();

//Clone object
$objcrs = clone $objcourse;
echo ”
The output of the primary object after clonning:

“;
//Name the tactic for $objcourse
$objcourse->Particulars();
echo ”
The output of the clonned object:

“;
//Name the tactic for $objcrs
$objcrs->Particulars();

?>

You’ll get the next output after operating the script from the webserver. Right here, when $objcrs is cloned, its worth is modified by the __clone() technique, however the worth of $objcourse stays unchanged. The worth of $objcourse is printed earlier than and after cloning. The worth of $objcrs is printed later.

How to clone an object in PHP – Linux Hint

Conclusion

Cloning is a helpful characteristic of PHP that can be utilized to create a replica of an object. Each the clone key phrase and the __clone() technique are proven on this tutorial by way of easy examples. We hope this tutorial helped you perceive object cloning.

linux install best practices,linux security best practices,how to secure root user in linux,security plan for linux server,ubuntu server best practices,service improvement plan for linux servers,void linux network manager,void linux enable service,fish shell void linux,void linux touchpad not working,void linux xmonad,void linux i3

About Author

admin

https://server-land.com