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 can also be used to override properties and methods in a class.
When referencing these items outside of the class definition, use the class name.
Since PHP 5.3.0, classes can be referenced through variables whose values cannot be keywords (such as self, parent and static).
The choice of Paamayim Nekudotayim as the name of the double colon operator seems a bit strange. However, this was a decision the Zend development team made when writing Zend Engine 0.5 (used in PHP 3). In fact, this word in Hebrew means double colon.
Example #1 Using :: operator
<?php class MyClass { const CONST_VALUE = 'A constant value'; } $classname = 'MyClass'; echo $classname::CONST_VALUE; // 自 PHP 5.3.0 起 echo MyClass::CONST_VALUE; ?>
<?php class OtherClass extends MyClass { public static $my_static = 'static var'; public static function doubleColon() { echo parent::CONST_VALUE . "\n"; echo self::$my_static . "\n"; } } $classname = 'OtherClass'; echo $classname::doubleColon(); // 自 PHP 5.3.0 起 OtherClass::doubleColon(); ?>
<?php class MyClass { protected function myFunc() { echo "MyClass::myFunc()\n"; } } class OtherClass extends MyClass { // 覆盖了父类的定义 public function myFunc() { // 但还是可以调用父类中被覆盖的方法 parent::myFunc(); echo "OtherClass::myFunc()\n"; } } $class = new OtherClass(); $class->myFunc(); ?>
The above introduces the range parsing operator (::), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.