In PHP, the double colon refers to the scope-limited operator, which can be used to access static members, that is, using variables to represent the class, and then using the double colon to access the static members outside the class. , the syntax is "test::$static property" or "test::static method".
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
Double colon operator: The Scope Resolution Operator can access static, const and overridden properties and methods in classes.
1. Use variables to access static members
In fact, it is to use variables to represent the class, and then use double colons to access the static members outside the class.
<?php class Fruit{ const CONST_VALUE='fruit color'; } $classname='Fruit'; echo $classname::CONST_VALUE;//fruit color ?>
When accessing yourself, replace the class name with $SELF, for example:
<?php class Fruit { const CONST_VALUE = 'Fruit Color'; } class Apple extends Fruit { public static $color = 'Red'; public static function doubleColon() { echo parent::CONST_VALUE . "\n"; echo self::$color . "\n"; } } Apple::doubleColon();//Fruit Color Red ?>
2. Use parent access
to access the parent class method.
<?php class Fruit { protected function showColor() { echo "Fruit::showColor()\n"; } } class Apple extends Fruit { // Override parent's definition public function showColor() { // But still call the parent function parent::showColor(); echo "Apple::showColor()\n"; } } $apple = new Apple(); $apple->showColor(); ?>
Running results:
Fruit::showColor()
Apple::showColor()
Recommended learning: "PHP Video Tutorial 》
The above is the detailed content of What is the usage of double colon in php. For more information, please follow other related articles on the PHP Chinese website!