The scope resolution operator (also known as Paamayim Nekudotayim) or, more simply, a pair of colons, can be used to access static members, class constants, and to override properties and methods in a class.
Use the class name when referring to these items outside of the class definition.
From PHP 5.3.0, the class can be referenced by a variable, and the value of the variable cannot be a keyword (such as self,parent and Static).
It seems strange to choose Paamayim Nekudotayim as the name of the double-colon operator. However, this is the decision made by the Zend development team when writing Zend Engine 0.5 (used in PHP 3). In fact, the word in Hebrew is the meaning of a double colon.
Example #1 used outside the class:: operator
Class myclass{ Const CONST_VALUE = 1;} $classname = ' MyClass '; Echo $classname:: Const_value; From PHP5.3.0 Echo myclass::const_value;
Self,parent and static These three special keywords are used to access their properties or methods within the class definition.
Example #2 used inside the class definition::
function __autoload ($classname) { require_once ($classname. "). PHP "); Class Otherclass extends myclass{public static $my _static = 1; public static function Doublecolon () { echo parent::const_value. ' <br> '; echo Self:: $my _static, ' <br> '; }} $classname = ' Otherclass '; Echo $classname::d Oublecolon (); Otherclass::d Oublecolon ();
When a subclass overrides a method in its parent class, PHP does not invoke methods that have been overridden in the parent class. Whether the method of calling the parent class depends on the child class. This mechanism is also used for constructors and destructors, overloading, and magic methods.
Example #3 Calling a method of the parent class
Class myclass{ protected function MyFunc () { echo ' myclass::myfunc () <br> '; }} Class Otherclass extends myclass{// overrides the definition of the parent class public function MyFunc () { //But can still call the overridden method in the parent class Parent::myfunc (); Echo ' Otherclass::myfunc () <br> '; }} $class = new Otherclass (), $class-MyFunc ();
Output Result:
Myclass::myfunc ()
Otherclass::myfunc ()