A detailed explanation of the shallow copy and deep copy in PHP

Source: Internet
Author: User
An example of shallow copy and deep copy in PHP

Objective:

Having recently studied the design pattern, we have noticed a shallow copy and deep replication problem when we see the prototype pattern. Here to summarize, remind yourself must pay more attention.

Since PHP5, the new operator automatically returns a reference, an object variable that no longer holds the value of the entire object, but only an identifier that accesses the actual object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, the other variable is not a reference to the original, except that they hold a copy of the same identifier, which points to the true content of the same object.

Here's a chestnut:

Class example1{public  $name;  Public function construct ($name)  {    $this->name = $name;  }} $ex 1 = new Example (' test1 ');//$ex 1->name is now: test1$ex2 = $ex 1;//$ex 2->name now: test1$ex2->name = ' test2 ';// After this modification, the $ex 1->name and $ex2->name are changed: test2

From the above example, we should be able to understand the concept of inter-object reference, then we continue to go down, in PHP to provide the clone of the keyword for object replication, or use the above class to demonstrate:

$ex 1 = new Example (' test1 ');//$ex 1->name is now: Test1$ex2 = Clone $ex 1;//$ex 2->name now: test1$ex2->name = ' test2 ' ;//Now $ex1->name or test1, and $ex2->name is Test2

As you can see here, after cloning, $ex 1 and $ex2 are two different objects, and they have their own variable environment. However, it is important to note that within these two objects, you have data of value type, and if you have a reference type internally, the reference in the new object obtained through clone still points to the original reference. Here, the concept of shallow copy and deep replication is derived:

Shallow copy : Use Clone to copy the object, which is called "shallow copy," and all the variables of the assigned object have the same value as the original object, and all references to other objects still point to the original object.

deep copy : All the variables of the copied object contain the same value as the original object, removing the variables that refer to other objects.

The default use of clone is to make a shallow copy, so how can deep copy be done?

Mode one: Using the Clone method

Public Function Clone () {  $this->obj = new obj ();}

This is a very intuitive approach, but there is a cumbersome way to do this, that is, when a class contains multiple references, you need to re-set them in the Clone method one at a time. But also to deal with some circular reference problems. is very complex.

Mode two: Using serialization (refrigeration and thawing)

$tmp = Serialize ($ex 1), $ex 2 = unserialize ($tmp);

This time the $EX2 is a completely new object, the process in Java is also called "Refrigeration" and "thaw" process.

Serialization is a recursive process, and we do not need to ignore how many objects are referenced inside the object and how many layers of objects are referenced, and we can replicate them completely. Way two really very yellow very violent, but I like it very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.