PHP object-oriented programming: special usage of "$ this" _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming: special usage of "$ this. PHP object-oriented programming (OOP): The special usage of "$ this" Now we know how to access members in an object through the "object-member" method, this is a special usage of "$ this" in PHP object-oriented (OOP) programming outside the object.

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:

 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; // The method for accessing the $ p1 object $ p1-> say (); // the following three rows assign $ p2 object attributes $ p2-> name = "Li Si"; $ p2-> sex = "female"; $ p2-> age = 30; // the following method for accessing the $ p2 object $ p2-> say (); // the following three rows assign a value to the $ p3 object attribute $ 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

Pipeline (OOP) programming: the special usage of "$ this". now we know how to access members in an object through the "object-member" method, this is done outside the object...

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.