When accessing a member variable or method in a PHP class, if the referenced variable or method is declared as const (defining a constant) or static (declaring static), then the operator must be used::, conversely, if the referenced variable or method is not declared as const or static, Then you must use the operator---.
In addition, if you access a const or static variable or method from within a class, you must use self-referencing, and conversely, if accessing from within a class is not a const or static variable or method, you must use a self-referencing $this.
$this instances
| The code is as follows |
Copy Code |
This is a pointer to the current object Class test_this{ Private $content; Defining variables
function __construct ($content) {//define Constructor $this->content= $content; } function __destruct () {}//defines a destructor
function Printcontent () {//define print function echo $this->content. ' '; } } $test =new test_this (' Beijing welcomes you! '); Instantiating an Object $test->printcontent ();//BEIJING welcome you! |
:: How to use
| code as follows |
copy code |
The parent is a pointer to the parents class Class test_parent{//Base classes Public $name; Define name the parent class member needs to be defined as public before it can be invoked directly in the inheriting class. function __construct ($name) { $this->name= $name; } } Class Test_son extends test_parent{//derived classes inherit test_parent public $gender;//define Gender Public $age; Define Age function __construct ($gender, $age) {//constructor for inheriting class Parent::__construct (' nostop ');//The constructor of the parent class is invoked with parent 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 '. ' '; } } $nostop =new Test_son (' Female ', ' 22 ');//Instantiate Test_son Object $nostop->print_info ();//execute output function nostop is a woman, 23 years old this year. |
Use the self:: $name form. Note that the Const attribute's declaration format, const PI=3.14, is not const $PI =3.14
| The code is as follows |
Copy Code |
Class Clss_a {
private static $name = "Static Class_a";
Const pi=3.14; Public $value;
public static function GetName () { Return self:: $name; } This is incorrect, static methods cannot access non-static properties public static function GetName2 () { Return self:: $value; } Public Function GETPI () { Return self::P i; }
} |
It is also important to note that if the method of the class is static, the property that he accesses must also be static.
When an internal method of a class accesses an attribute that is not declared as const and static, use the form $this->value = ' class_a ';
http://www.bkjia.com/PHPjc/628859.html www.bkjia.com true http://www.bkjia.com/PHPjc/628859.html techarticle when accessing a member variable or method in a PHP class, if the referenced variable or method is declared as const (defining a constant) or static (declaring static), then the operator must be used: ...