Php deep Replication

Source: Internet
Author: User
{Code ...}
phpClass test {public $ num = 100;} $ a = new test (); $ B = clone $ a; $ a-> num = 200; echo $ B-> num; // 100 class test {public $ num = 100;} $ a = new test (); $ B = $ a; $ a-> num = 200; echo $ B-> num; // 200 // array $ a = [, 3]; $ B = $ a; $ a [1] = 5; var_dump ($ B); // [1, 2, 3] $ a = [1, 2]; $ B = & $ a; $ a [1] = 5; var_dump ($ B); // [, 3] // The above is the php deep copy, light copy, right?

Reply content:
phpClass test {public $ num = 100;} $ a = new test (); $ B = clone $ a; $ a-> num = 200; echo $ B-> num; // 100 class test {public $ num = 100;} $ a = new test (); $ B = $ a; $ a-> num = 200; echo $ B-> num; // 200 // array $ a = [, 3]; $ B = $ a; $ a [1] = 5; var_dump ($ B); // [1, 2, 3] $ a = [1, 2]; $ B = & $ a; $ a [1] = 5; var_dump ($ B); // [, 3] // The above is the php deep copy, light copy, right?

Class SubTest {public $ num = 100;} class Test {public $ child; function _ construct ($ child) {$ this-> child = $ child ;}} $ a = new Test (new SubTest (); // perform a shallow copy here $ B = clone $ a; $ B-> child-> num = 200; // The output below is 200 echo $ a-> child-> num, "\ n"; echo $ B-> child-> num, "\ n "; class Test2 {public $ child; function _ construct ($ child) {$ this-> child = $ child;} // this method is called when the clone is complete, complete the deep copy operation function _ clone () {$ this-> child = clone $ this-> child ;}}$ a = new Test2 (new SubTest () here ()); // perform deep copy here $ B = clone $ a; $ B-> child-> num = 200; // The output below is 100,200 echo $ a-> child-> num, "\ n"; echo $ B-> child-> num, "\ n ";

Reference PHP: Object Replication

The subject's Code contains copying and referencing two conceptual objects. See objects and references.

It is difficult to describe deep replication and light replication, and the Code on the upstairs also shows the difference. Two intuitive images are presented, so that we can better understand it.
Light Replication:

Deep Replication:

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.