[Reprint]php medium-deep copy shallow copy

Source: Internet
Author: User

Transferred from: http://cnn237111.blog.51cto.com/2359144/1283163

PHP provides an object copy operation, clone. The syntax is quite simple:

$a = Clone $b;

1. Shallow copy

The above expression gets the A object that duplicates the B object. If the members in the B object are value types, then there is no relation, and the members in the A object and the members in the B variable occupy separate memory space. But since this cloning operation is a shallow copy , if the member of B has a reference type of data, then the member of the A object does not actually replicate the member, but instead shares the object with the members of the B object. Look at the following example.

<?phpclass a{public    $info = "the Is A";} Class b{public    $a;    function __construct ()    {        $this->a=new A;    }    Public $info = "This is B";} $b 1=new B (); echo "clone operation \ n", $b 2=clone $b 1;echo "B1 value \ n", echo "B1 info:{$b 1->info}\n"; echo "B1 info:{$b 1->a- >info}\n "; echo" B2 value \ n ", echo" B2 info:{$b 2->info}\n "; echo" B2 info:{$b 2->a->info}\n "; $b 1->info=" This value was updated (this is B) "; $b 1->a->info=" The "This value was updated (this is a)"; echo "after modifying B1, the value of B1 \ n"; echo "B1 info: {$b 1->info}\n "; Echo" B1 A's info:{$b 1->a->info}\n "; echo" Modified B1, B2 value \ n "; echo" B2 info:{$b 2->info}\n "; echo "B2 A's info:{$b 2->a->info}\n"; echo "Determines whether a and B2 of B1 are the same object:", $b 1->a=== $b 2->a, "\ n";

Output Result:

Clone action
The value of the B1
B1 Info:this is b
B1 A's info:this is a
The value of the B2
B2 Info:this is b
B2 A's info:this is a
After modifying B1, the value of B1
B1 Info:this value is updated (this is B)
Info:this value of B1 A is updated (this is a)
After modifying B1, the value of B2
B2 Info:this is b
Info:this value of B2 A is updated (this is a)
Determines whether a and b2 A of B1 are the same object: 1

As you can see, after modifying the value of reference type A in B1, the value of a in B2 also changes. Further, it can be judged that B1 A and B2 A are the same object.

2. Deep copy

Like C + +, PHP also provides copy constructors, which allow you to customize replication behavior for deep copies. PHP accomplishes the copy constructor by implementing the __clone () method in the definition of the object. This function is called when the object is copied. Or the previous code, modify it.

<?phpclass a{public    $info = "the Is A";} Class b{public    $a;    function __construct ()    {        $this->a=new A;    }    Public $info = "it is B";    Public Function __clone ()    {        echo ' copy constructor starts calling <br> ";        $new _object=new A;        $new _object->info= $this->a->info;        $this->a= $new _object;    }} $b 1=new B (); echo "clone operation \ n", $b 2=clone $b 1;echo "B1 value \ n", echo "B1 info:{$b 1->info}\n"; echo "B1 info:{$b 1->a- >info}\n "; echo" B2 value \ n ", echo" B2 info:{$b 2->info}\n "; echo" B2 info:{$b 2->a->info}\n "; $b 1->info=" This value was updated (this is B) "; $b 1->a->info=" The "This value was updated (this is a)"; echo "after modifying B1, the value of B1 \ n"; echo "B1 info: {$b 1->info}\n "; Echo" B1 A's info:{$b 1->a->info}\n "; echo" Modified B1, B2 value \ n "; echo" B2 info:{$b 2->info}\n "; echo "B2 A's info:{$b 2->a->info}\n"; echo "Determines whether a and B2 of B1 are the same object:", $b 1->a=== $b 2->a, "\ n";? >

Output Result:

Clone action
The copy constructor starts calling the value of <BR>B1
B1 Info:this is b
B1 A's info:this is a
The value of the B2
B2 Info:this is b
B2 A's info:this is a
After modifying B1, the value of B1
B1 Info:this value is updated (this is B)
Info:this value of B1 A is updated (this is a)
After modifying B1, the value of B2
B2 Info:this is b
B2 A's info:this is a
Determine whether a and b2 A of B1 are the same object:

Finally, you can see that B1 A and b2 A are false, so an empty string is printed.

————————————————————————

The above method realizes the Magic method __clone, in this method defines own deep copy method, this kind of writing is more troublesome, if the object modifies, this method also has to revise. In fact, deep copies of the members can take the form of serializing the object and then restoring it. This possibility can be lost, but it is the most convenient. In PHP, deep copies are implemented using the following statement:

  $b2 = unserialize(serialize($b1)); //序列化然后反序列化

[Reprint]php medium-deep copy shallow copy

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.