PHP:,->, self, $ this operator _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
In PHP:,-& gt;, self, $ this operator. 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: 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, the operator-> must be used.

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.

$ This instance

The code is as follows:

// This is a pointer to the current object

Class test_this {
Private $ content; // defines the variable

Function _ construct ($ content) {// define the constructor
$ This-> content = $ content;
}
Function _ destruct () {}// define the destructor

Function printContent () {// defines the print function
Echo $ this-> content .'
';
}
}

$ Test = new test_this ('welcome to Beijing! '); // Instantiate the object
$ Test-> printContent (); // Welcome to Beijing!

: Usage

The code is as follows:

// Parent is a pointer to the parent class

Class test_parent {// base class
Public $ name; // define the name. the parent class member must be defined as public before this can be called directly in the inherited class.
Function _ construct ($ name ){
$ This-> name = $ name;
}
}
Class test_son extends test_parent {// the derived class inherits test_parent
Public $ gender; // defines the gender.
Public $ age; // defines the age.
Function _ construct ($ gender, $ age) {// inherited class constructor
Parent: :__ construct ('nostop'); // 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 a'. $ this-> gender. ', this year'. $ this-> age. 'years old '.'
';
}
}

$ Nostop = new test_son ('female ', '22'); // instantiate the test_son object
$ Nostop-> print_info (); // executes the output function nostop, 23 years old.


Use the form of self: $ name. Note the declarative format of the const attribute. const PI = 3.14 instead of const $ PI = 3.14.

The code is as follows:

Class clss_a {

Private static $ name = "static class_a ";

Const PI = 3.14;
Public $ value;

Public static function getName ()
{
Return self: $ name;
}
// This statement is incorrect. the static method cannot access non-static attributes.
Public static function getName2 ()
{
Return self: $ value;
}
Public function getPI ()
{
Return self: PI;
}


}

Note that if the class method is static, the Accessed attribute must also be static.
$ This-> value = 'class _ A'; is used when the internal methods of the class are not declared as const or static.

Declare (defining constants) or static (declaring static), you must use the operator :...

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.