How to understand object references in PHP, shallow copy and deep copy

Source: Internet
Author: User
Tags print object
This article is to share with you about how to understand the object reference in PHP, shallow copy and deep copy, the need for friends can refer to

Many novice phper to OOP programming is easy to ignore these concepts, the recent reading of the junior PHP engineer this part of the object reference is more vague, looked up some information, understand, want to do a record here, tell yourself that these foundations must be consolidated.

Reference article: https://blog.csdn.net/hel12he/article/details/49617023

https://blog.csdn.net/koastal/article/details/52163483

First we should understand how the memory space in PHP is distributed. In general, memory space is divided into heap memory, stack memory, data segment, code snippet , such as:

So what is a reference to an object?

When we instantiate a class with the new keyword, we can access the properties and methods in that class through this object. And each object is independent, for example, a car class has a size, color and other properties, there are driving methods, this class can be instantiated into multiple objects, each object's property method can be modified, that is, the object has its own memory space, which contains the instance of the class's properties and methods, Referencing an object, or using an alias, actually points to the same memory space, for example:

<?php  class one{public    $name;    Public function __construct ($name)    {        $this->name = $name;    }} Instantiate, and pass the parameter $o1 = new One (' test1 ') to the constructor, and echo "Object 1 has the name value:". $o 1->name. " <br/> ";//reference $o2 = $o 1;//Print Object 2 $name property echo" Object 2 has the name value: ". $o 2->name." <br/> ";//Modify the $name property of the object $o2 echo" modifies the name of object 2 to Test2 "." <br/> "; $o 2->name = ' test2 ';//Compare the $name value of two objects at this time echo" The name value of the modified Object 1 is: ". $o 1->name." <br/> "; echo" after the modified object 2, the name value is: ". $o 2->name." <br/> ";

Printing results:

As a matter of fact, the reference is essentially just an alias of the object, and the memory space that the referenced object and the reference object point to is still the same. (The effect of $o2=&o1 here is the same).


So what is shallow copy?

Concept: copy objects using clone, 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. that is, a shallow copy simply duplicates the object being considered, not the object it refers to.

We use the same example to change the $o2= $o 1 to $o2=clone $o 1. See what happens. Printing results such as:


As you can see, object 1 does not change when we only modify the value of object 2, because using the Clone keyword generates a copy of an object that has its own memory space, which is previously independent of each other. But a shallow copy does not copy the object it refers to.


What's in front of you is that the content is only numeric, and when the content has references to objects that are not the same, it leads to the concept of deep replication.

Deep copy concept: All variables of the copied object contain the same value as the original object, removing the variables that refer to other objects. That is, a deep copy copies the objects that are referenced by the object being copied over.

Two methods:

① using __clone Magic method

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


② the use of serialization

$t = Serialize ($o 1), $o 2 = unserialize ($t);


a chestnut:

<?phpheader ("Content-type:text/html;charset=utf-8"); class Tvcontrol{}class tv{    private $color;    Private $tvControl;    function __construct () {        $this->color = "BLACK";        $this->tvcontrol = new Tvcontrol ();    }        function Gettvcontrol () {        return $this->tvcontrol;    }} $TV 1 = new Tv (), $tvControl 1 = $tv 1->gettvcontrol (); echo "Original class:". " <br/> "; Var_dump ($tv 1); echo" <br/> "; $tv 2 = $tv 1;echo" Reference class: "." <br/> "; Var_dump ($tv 2); echo" <br/> "; $tv 3 = Clone $tv 1;echo" clone (Shallow copy): "." <br/> "; Var_dump ($tv 3); echo" <br/> "; $tv 4 = unserialize (serialize ($TV 1)); echo" Deep copy: "." <br/> "; Var_dump ($TV 4);

Printing results:


Deep copy gets to an object that is also an entirely new object.

So how do you compare two objects to a single object?

I can use "= = =" To compare whether two objects are referenced to the same initial object. the "= =" can only compare two objects with the same class and properties.

Related recommendations:

In-depth understanding of the count function of PHP arrays

Deep understanding of PHP looping statements


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.