Php light replication and deep Replication

Source: Internet
Author: User
During the weekend, I had nothing to do with the prototype mode. I talked about light replication and deep replication, and thought about the corresponding assignment, cloning, and cloning in PHP. Let's take a look at the assignment. For example, there is a resume class with two attributes: height and weight: classResume {public $ height; public $ weight; public $ workExperience;} $ Resu

During the weekend, I had nothing to do with the prototype mode. I talked about light replication and deep replication, and thought about the corresponding assignment, cloning, and cloning in PHP. Let's take a look at the assignment. For example, there is a Resume class with two attributes: height and weight: class Resume {public $ height; public $ weight; public $ workExperience;} $ Resu

During the weekend, I had nothing to do with the prototype mode. I talked about light replication and deep replication, and thought about the corresponding assignment, cloning, and cloning in PHP.

Let's take a look at the assignment. For example, there is a resume class with two attributes: height and weight:

class Resume {    public $height;    public $weight;    public $workExperience;}$ResumeA = new Resume();$ResumeB = $ResumeA;

A Resume class is instantiated and assigned to the $ ResumeA variable. Then, the $ ResumeA variable is assigned to $ ResumeB. The PHP manual says:

Since PHP5, the new operator automatically returns a reference, and an object variable no longer saves the value of the entire object. Only saves an identifier to access the real object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, the other variable is not referenced, but they all store copies of the same identifier, this identifier points to the real content of the same object.

Therefore, if you use $ ResumeB to modify the height attribute, $ ResumeA will also change. If you want to copy a brand new object, you can clone it, for example:

$ResumeB = clone $ResumeA;

Copy the value of $ ResumeA to the new variable $ ResumeB, and change one of them without affecting the other. Modify the height attribute in $ ResumeB and $ ResumeA will not change.
However, if this class references other objects, all references still point to the original object. The clone replication method is shortest replication.All 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.

If workExperience in the above class is a reference of the WorkExperience class, when cloning, the workExperience attribute Before and After cloning still points to the same object content.

Deep replication corresponds to the shallow replication,Deep replication points the variables of the referenced object to the copied new object instead of the original referenced object.

Deep replication can be achieved in PHP in two ways. The first method is _ clone magic:

public function __clone(){    $this->workExperience = new WorkExperience();}

Deep Replication involves deep layers. To implement the clone magic method, you need to know how many layers are available and then implement each layer in sequence. Another method is to serialize an object before deserializing it. For example:

$ResumeB = unserialize(serialize($ResumeA));

Clone is a common COPY method. The purpose of sorting is to record that clone is a shortest copy. Pay attention to object references.

Original article address: php light copy and deep copy. Thank you for sharing it with the original author.

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.