PHP Object-Oriented Programming: special usage of "$ this"

Source: Internet
Author: User

PHP Object-Oriented Programming: special usage of "$ this"

Now we know how to access the members in an object. They are accessed through "Object-> member". This is the form of accessing the members in an object outside the object, so what if I want to allow methods in the object to access the attributes of the object, or call other methods of the object through methods in the object? Because all the members in the object must be called by objects, including calls between internal members of the object, a reference to this object is provided in PHP. $ this, each object has an object reference $ this to represent this object and complete the call of its members. this is meant to be "this". In the above instance, we instantiate three instance objects $ P1, $ P2, and $ P3, each of which has a $ this representing the objects $ p1, $ p2, and $ p3 respectively.

We can see that $ this refers to the reference of this object inside the object. It is used in the same way as the members of the calling object and the members of the calling object outside the object.

$ 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:

<? Phpclass 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; // The age of a person // The following is the member method of a person: function say () {// the way 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 $ p1 object attributes $ p1-> name = "zhangsan"; $ p1-> sex = "male "; $ p1-> age = 20; // access the following $ p 1. The method $ p1-> say () in the object; // The following three rows assign a value to the $ p2 object attribute $ 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 $ 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: 40

Analyze this method:

Function say () {// the way this person can talk 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. If you call the run () method in the say () method, you can also () in this method, $ this-> run () is used to complete the call.

Articles you may be interested in
  • PHP object-oriented tutorials
  • PHP object-oriented rules
  • PHP Object to Array (Object to Array), Json to Array (Json to Array) Method
  • Php uses curl to implement multi-threaded classes and php curl to download images with multiple threads
  • A classic dialogue between programmers and testers. These are shared by foreign programmers, who say they are used globally?
  • Analysis on abstract classes and abstract methods in php
  • How to compress gzip pages using PHP
  • Php determines whether the string is full of English, pure Chinese, and a combination of Chinese and English

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: 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.