What is the operator "::"?
Operator "::" is more powerful than pseudo-variable $this only in the internal use of the class. The operator "::" can access a member method in a class without any declaration of any instance of a live member variable.
Operator "::" Syntax:
The general syntax for using the "::" operator is:
Keyword:: variable name/constant name/method name
Precautions:
The keywords here are no longer public, private, protected or static, but the following three scenarios:
(1) Parent: You can call member variables, member methods, and constants in the parent class;
(2) Self: can invoke static members and constants in the current class;
(3) Class name: You can call variables, constants, and methods in this class.
Friendly reminder:
For more information about static variables (methods), please refer to the article on this site: http://www.php.cn/php-weizijiaocheng-360326.html
The above simple introduction of the following operator "::" The basic content, below we have a simple example to more specific understanding of the operator "::" usage.
<?phpheader ("Content-type:text/html;charset=utf-8"); class hero{ //define class Const CHARACTER = ' Anna '; Define constants, using the keyword const function __construct () //constructor in parent class { echo ' 5 floor to play in single '. Hero:: CHARACTER. ' Send it without giving '; Output constant echo ' <br/> '; }} class I_hreo extends hero{ //Create subclass I_hreo Inherit parent class const ROLE = ' ya '; Create a const function __construct () //subclass in Create constructor { Parent:: __construct (); Referencing a constructor in a parent class echo ' 4 floor in single '. Self:: ROLE. ' Thief 6 '; Sub-class Output }} $gamer = new I_hreo (); Instantiation of
Example Explanation:
In the above example, we first created a parent class hero, defined the constants, created the constructors, and then created a subclass I_hreo to inherit the parent class Hreo, and also defined constants in the subclass. So here's the point, we're referencing the constructors in the parent class. We've covered three cases, parent: You can call member variables, member methods, and constants in the parent class. Here we call the methods in the parent class, so use the parent:: __construct (). Next, we output the constants defined in the subclass in the constructor method of the subclass, invoke the keyword of the current class, as described above, self: can call static members and constants in the current class, so here we use self:: ROLE, finally formatted, run a bit.
Results See: