$ This and $ that pointers in PHP use instances

Source: Internet
Author: User
This article mainly introduces the $ this and $ that pointer usage instances in PHP. It can also be said that they have different usage details. the instance can be understood at a glance, you can refer to PHP5 to define a special method name "_ clone ()", which is automatically called during object cloning, use the "_ clone ()" method to create an object with the same attributes and methods as the original object. if you want to change the content of the original object after cloning, you need to override the original attributes and methods in _ clone (). The "_ clone ()" method can have no parameters. it automatically contains the $ this and $ that pointers, $ this points to the duplicate, while $ that points to the original. the specific instance is as follows:

The code is as follows:


<? Php
Class Person {
// The following are the member attributes of a person.
Var $ name; // The name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// 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;
}
// This person can talk about his/her attributes.
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, so that the attributes of the copy are changed in this method.
$ This-> name = "I'm copying Michael Jacob $ that-> name ";
// $ This-> age = 30;
}
}
$ P1 = new Person ("zhang san", "male", 20 );
$ P2 = clone $ p1;
$ P1-> say ();
$ P2-> say ();
?>

The result of successfully running this PHP program is as follows:

The code is as follows:


My name is Zhang San Gender: Male my age is: 20
My name is: I copied the three Gender: Male, my age is: 20

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.