Use the $ this, static, final, const, and self keywords in the php object-oriented class. Functions of the three keywords "this, self, and parent" in php
The difference between the three keywords "this, self, and parent" is literally better understood, namely, "yourself" and "father. Let's first establish several concepts. where are these three keywords used? We will give a preliminary explanation. this is the pointer to the current object (let's take a look at the pointer in C). self is the pointer to the current class, and parent is the pointer to the parent class. We often use pointers to describe them here, probably because there is no better language to express them.
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 (); // Welcome to Beijing! $ Test = new test_this ('New Year's new weather! '); // Instantiate the object $ test-> printContent (); // New Year's weather! Echo'
'; // Self refers to the class itself and is only related to the class. it has nothing to do with any object instance. class test_self {private static $ first_count; // defines the static variable private $ last_count; function _ construct () {$ this-> last_count = ++ self: $ first_count; // directly use self to call the value of the variable and assign the value to another variable} function _ destruct () {} function print_self () {print ($ this-> last_count );}} $ abc = new test_self (); // instantiate the object $ abc-> print_self (); // 1 echo'
'; // Parent is the pointer to the parent class test_parent {// the base class public $ name; // defines the name of the parent class member to be defined as public, this can be called directly in the inheritance 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) {// Constructor parent of the inherited class :: __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 '. $ 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?>
$ This indicates the current instance. when the internal method access of the class is not declared as a const or static attribute, $ this-> value = 'phpernote'; is used. Common usage:
$ This-> attribute
$ This-> method
Example:
View code printing
Name = $ name;} public function getname () {return $ this-> name;} public function printName () {echo $ this-> getname ();}} $ myclass = new MyClass ("I Like PHP"); $ myclass-> printName (); // output: I Like PHP?>
There are three methods to call the attributes and methods of the current class in the class: self, parent, and $ this. The difference between the three keywords is: self is used to direct to the current class; parent is used to point to the parent class of the current class. you can use this keyword to call the attributes and methods of the parent class. $ this is used to call its own attributes and methods in the class body.
The static keyword can be the class name of the static member (used when the static member is called inside the class) where the static member is located (used when the static member inside the class is called outside the class)
Declare a static variable as follows:
Static $ val = '';
Variables only exist in the function scope. after the function is executed, the value of the variable will not be lost and will only be initialized once. the expression cannot be used to initialize static variables, you do not need to replace global variables because they are easily accessed by all functions and thus are not suitable for maintenance.
Using static in a class has two main purposes: defining static members and defining static methods. Static members only keep the value of one variable. this value is valid for all instances, as shown below:
MyMethod (); $ instance2 = new MyObject (); $ instance2-> myMethod (); // The results are printed 2 and 4 respectively.
ShowMe (); echo"
"; $ Book2 = new Book (); $ book2-> showMe (); echo"
"; Echo" you are a drop ". Book: $ num." guest ";?>
The result will be:
You are a visitor of 0
You are a visitor.
You are two visitors
In addition, it should be noted that if the class method is static, the Accessed attribute must also be static.
FinalThe final class and method cannot be inherited, and the method modified by this keyword cannot be overwritten. The general usage is as follows:
ConstWhen you access the attributes declared as const and static by using internal methods of the class, you must use self ::$ name to call them. Example:
Note that the declarative format of the const attribute is const PI = 3.14, rather than const $ PI = 3.14.
Self Self indicates the class itself, pointing to the current class. It is usually used for static members, methods, and constants of the category class.
Differences between:,->, self, and $ this operators in PHPWhen 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.
PHP double colon: usage The dual-colon Operator is the Scope limitation Operator. Scope Resolution Operator can access static, const, and attributes and methods that are rewritten in the class.
Use the class name for calling when it is used outside the class definition. In PHP 5.3.0, you can use variables instead of class names. Program List: use variables to define external access in the class.
Program List: used outside the class definition ::
Program running result: Fruit Color Red
Program List: call the parent method
showColor();?>
Program running result:
Fruit: showColor ()
Apple: showColor ()
Program List: Use the scope qualifier
color; } } class Banana { public $color; public function __construct() { $this->color = "Banana is yellow"; } public function GetColor() { return Apple::showColor(); } } $banana = new Banana; echo $banana->GetColor();?>
Program running result: Banana is yellow Program List: call the method of the base class
Program running result: show color