Home > Article > Backend Development > Application of double colon in php
The "::" operator is often seen in PHP class codes. This is a scope limiting operator, which is represented by a double colon "::". It is used to set the levels of different scopes in the class. The left side is the scope and the right side is the members of the access scope.
There are two scopes defined in php: self and parent (static scope is provided in php6).
self: Represents the scope of the current class. Unlike this, it does not represent a specific instance of the class. Self cannot be used in code outside the class, and it cannot identify its own hierarchical position in inheritance. That is to say, when self is used in an extended class, it does not call the method of the parent class, but the overloaded method of the extended class.
parent: Indicates the scope of the parent class of the current class, and the rest is the same as the self attribute.
Example of PHP double colon::operator:
<?php class forasp{ static $url="http://blog.csdn.net/abandonship"; static $webname = "PHP学习之双冒号的用法"; public function writeurl(){ echo self::$url;//调用自己的内容 } public function writewebname(){ echo "测试子类调用父类内容"; } } class cn extends forasp{ function father(){ parent::wirtewebname(); } } $a = new forasp();//实例化父类 $a->writeurl();//调用自身内容 $b = new cn(); $b->writewebname();//调用父类内容 ?>
You can also use :: when calling static methods to call static methods or properties in a class, which can reduce resource usage because each instance of the class will occupy part of the resources.
static:: scope is proposed in php6, so we no longer need self:: and parent::. When you want to point to the final class that implements the function, use static::. This qualifier will calculate the members of the last class in the inheritance layer immediately before the code is executed. This process is called delayed binding.
The "double colon operator", also known as the "Scope Resolution Operator", can access static, const, and overridden properties and methods in classes.
If used outside the class definition, use the class name to call. In PHP 5.3.0, you can use variables instead of class names.
Program List: Use variables to access outside the class definition
<?php class Fruit { const CONST_VALUE = 'Fruit Color'; } $classname = 'Fruit'; echo $classname::CONST_VALUE; // As of PHP 5.3.0 echo Fruit::CONST_VALUE; ?>
Program List: Use double colons (::) outside the class definition
<?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(); ?>
Program running results:
Fruit Color Red
Program List: Call parent Method
Php code
<?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(); ?>
Program running result:
Fruit::showColor()
Apple::showColor()
Program List: Use scope qualifier
Php Code
<?php class Apple { public function showColor() { return $this->color; } } class Banana { public $color; public function __construct() { $this->color = "Banana is yellow"; } public function GetColor() { return Apple::showColor(); } } $banana = new Banana; echo $banana->GetColor(); ?>
Program running result:
Banana is yellow
Program List: Method of calling base class
Php code
<?php class Fruit { static function color() { return "color"; } static function showColor() { echo "show " . self::color(); } } class Apple extends Fruit { static function color() { return "red"; } } Apple::showColor(); // output is "show color"! ?>
Program running result:
show color