Php object-oriented full strategy (3) Special reference of "$ this"

Source: Internet
Author: User
Now we know how to access the members in an object. they are accessed through "object-member". This is the form of accessing members in an object outside the object, so if I want to allow methods in the object to access the attributes of the object, or call methods in the object 7. special reference to the use of "$ this"
Now we know how to access the members in the object through "object-> Member ".
Object, if I want to allow methods in the object to access this object inside the object
What should we do when we call other methods of this object based on the object's attributes or methods? Because
Some members use objects to call, including calls between internal members of objects. Therefore, PHP provides
$ This is a reference of this object. each object has an object reference $ this to represent this object.
For internal member calls, this is meant to be "this". in the above instance, we instantiate three instances.
Objects $ P1, $ P2, and $ P3 each have one $ this representing the objects $ p1, $ p2, and $ p3.
We can see that $ this represents the reference of this object inside the object, inside the object and the call Book
The members of an object and the members of an external call object use the same method.
$ This-> attribute $ this-> name; $ this-> age; $ this-> sex;
$ This-> method $ this-> say (); $ this-> run ();
Modify the instance above to allow everyone to name themselves, gender and age:
Code snippet
The code is as follows:
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
// The following is the member method of a person.
Function say () {// method in which this person can speak
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
Function run () {// how this person can walk
Echo "this person is walking ";
}
}
$ P1 = new Person (); // Create an instance object $ p1
$ P2 = new Person (); // Create an instance object $ p2
$ P3 = new Person (); // Create an instance object $ p3
// The following three rows assign values to $ p1 object attributes
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// The following method of accessing the $ p1 object
$ P1-> say ();
// The following three rows assign values to $ p2 object attributes.
$ P2-> name = "Li Si ";
$ P2-> sex = "female ";
$ P2-> age = 30;
// The following method of accessing the $ p2 object
$ P2-> say ();
// The following three rows assign values to $ p3 object attributes
$ P3-> name = "Wang Wu ";
$ P3-> sex = "male ";
$ P3-> age = 40;
// The following method is used to access the $ p3 object.
$ P3-> say ();
?>

Output result
My name is Zhang San Gender: Male my age is: 20
My name is Li Si Gender: Female my age is: 30
My name is: Wang Wu Gender: Male my age is: 40
Analyze this method:
Code snippet
Function say () {// method in which this person can speak
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
The say () method is available for $ p1, $ p2, and $ p3 objects. $ this represents the three objects and calls the corresponding attributes, print the attribute value, which is the way to access the object attribute inside the object.
You can also call the run () method in the statement () method to complete the call by using the $ this-> run () method.

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.