PHP:,->, self, $ this operator

Source: Internet
Author: User
When accessing the member variables or methods in the PHP class, if the referenced variables or methods are declared as const (defining constants) or static (declaring static), the operator must be used:: otherwise, if the referenced variable or method is not declared as cons... when accessing the member variables or methods in the PHP class, if the referenced variables or methods are declared as const (defining constants) or static (declaring static), the operator must be used:: if the referenced variable or method is not declared as const or static, you must use the operator->.

In addition, if you access const or static variables or methods from the inside of the class, you must use self-referenced. Otherwise, if the internal access from the class is not a const or static variable or method, then you must use the self-referenced $ this.

The $ this instance code is as follows:

 Content = $ content;} function _ destruct () {}// defines the destructor function printContent () {// defines the print function echo $ this-> content .'
';}}$ Test = new test_this ('welcome to Beijing! '); // Instantiate the object $ test-> printContent (); // Beijing welcomes you!?>

: The usage example code is as follows:

 Name = $ name ;}} class test_son extends test_parent {// the derived class inherits test_parent public $ gender; // defines the gender public $ age; // define the age function _ construct ($ gender, $ age) {// Constructor parent ::__ construct ('nostop') of the inherited class '); // Use parent to call the constructor of the parent class to instantiate the parent class $ this-> gender = $ gender; $ this-> age = $ age ;} function _ destruct () {} function print_info () {echo $ this-> name. 'is '. $ this-> gender. ', this year '. $ this-> age. 'year '.'
';}}$ Nostop = new test_son ('female', '22'); // instantiate the test_son object $ nostop-> print_info (); // execution of the output function nostop is a female, 23 years old?>

Use the self: $ name format. note the statement format of the const attribute. const PI = 3.14 instead of const $ PI = 3.14.

The instance code is as follows:

 

Note that if the class method is static, the Accessed attribute must also be static.

Use $ this-> value = 'class _ A'; in the form of $ this-> value = 'class _ A' when the internal method of the class is not declared as const or static.


Article address:

Reprint at will ^ please include the address of this article!

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.