Php accesses the original object content during clone

Source: Internet
Author: User
$ P2 is cloned from $ p1. I want to output "I cloned from: Evan" in the _ clone () annotation. How should I write it? It seems that there is $ that, but it cannot be used. No introduction was found in Manual. The Code is as follows: {code ...} the expected result is: {generation... $ p2 is cloned from $ p1. I want to output "I cloned from: Evan" in the _ clone () annotation. How should I write it?
It seems that $thatThis is not usable. No introduction was found in Manual.
The Code is as follows:


  Name = $ name;} function _ clone () {$ this-> name = "WenryXu"; // echo "I cloned from :". name} function say () {echo "My name is :". $ this-> name."
";}}$ P1 = new Person (" Evan "); $ p1-> say (); $ p2 = clone $ p1; $ p2-> say ();

The expected result is:

My name is Evan. I cloned it from Evan. My name is WenryXu.

Thank you!

Reply content:

$ P2 is cloned from $ p1. I want to output "I cloned from: Evan" in the _ clone () annotation. How should I write it?
It seems that$thatThis is not usable. No introduction was found in Manual.
The Code is as follows:


  Name = $ name;} function _ clone () {$ this-> name = "WenryXu"; // echo "I cloned from :". name} function say () {echo "My name is :". $ this-> name."
";}}$ P1 = new Person (" Evan "); $ p1-> say (); $ p2 = clone $ p1; $ p2-> say ();

The expected result is:

My name is Evan. I cloned it from Evan. My name is WenryXu.

Thank you!

The Code is as follows.


  Name = $ name;} function _ clone () {echo "I cloned from :". $ this-> name. "\ n"; $ this-> name = "WenryXu";} function say () {echo "My name is :". $ this-> name. "\ n" ;}}$ p1 = new Person ("Evan"); $ p1-> say (); $ p2 = clone $ p1; $ p2-> say ();


  Name = $ name;} function _ clone () {echo "I cloned from :". $ this-> name. "\ n"; $ this-> name = "WenryXu";} function say () {echo "My name is :". $ this-> name. "\ n" ;}}$ p1 = new Person ("Evan"); $ p1-> say (); $ p2 = clone $ p1; $ p2-> say ();

In this way, you can
In fact, clone is just a small copy.
During replication, all the member variables in the object are referenced only.
Therefore, the original value can be obtained through direct calls.

Output
My name is Evan.
I cloned it from Evan.
My name is WenryXu

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.