What is the difference between the two $ this in the _ clone () method of php?

Source: Internet
Author: User
How to explain the two $ this in the _ clone () method of php? What are their meanings? {Code...}: how does one explain the two $ this in the _ clone () method of php? What are their meanings?

Class Person {// The following is the member attribute var $ name of a Person; // The name of a Person var $ sex; // gender var $ age of a Person; // person's age // define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age // function _ construct ($ name = "", $ sex = "", $ age = "") function _ construct ($ name, $ sex, $ age) {$ this-> name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // The method that this person can talk to and state his/her attribute function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}// Method automatically called during object cloning. if you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in _ clone. Function _ clone () {// $ this refers to the copy p2, and $ that points to the original p1. in this way, the attributes of the copy are changed. $ This-> name = "I am copying Michael Jacob $ this-> name"; // $ this-> age = 30 ;}} $ p1 = new Person ("zhang san", "male", 20); $ p2 = clone $ p1; $ p1-> say (); $ p2-> say ();

Reply content:

How to explain the two $ this in the _ clone () method of php? What are their meanings?

Class Person {// The following is the member attribute var $ name of a Person; // The name of a Person var $ sex; // gender var $ age of a Person; // person's age // define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age // function _ construct ($ name = "", $ sex = "", $ age = "") function _ construct ($ name, $ sex, $ age) {$ this-> name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // The method that this person can talk to and state his/her attribute function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}// Method automatically called during object cloning. if you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in _ clone. Function _ clone () {// $ this refers to the copy p2, and $ that points to the original p1. in this way, the attributes of the copy are changed. $ This-> name = "I am copying Michael Jacob $ this-> name"; // $ this-> age = 30 ;}} $ p1 = new Person ("zhang san", "male", 20); $ p2 = clone $ p1; $ p1-> say (); $ p2-> say ();

__clone()No two$this, Only one$this, This$thisThe point is the cloned new object, because__clone()The method is called in the new object.
During cloning, PHP will first perform a light copy to create an object and copy all the attributes of the original object to the new object. For reference variables such as objects and resources, just copy their pointers without cloning them. If you need to perform deep replication on these attributes, you must__clone()Clone it separately.
For example:

class MyCloneable{    public $obj;        function __clone()    {        $this->obj = clone $this->obj;    }}

Note:$this->objAll refer to the new class$objAttribute, because the new object$objThe original object has been shorily copied. this is because we want to perform deep replication$this->objThen, a clone is executed.

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.