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, a variable can be used to refer to a class, 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
<?php
class MyClass {
const CONST_VALUE = ‘A constant value‘ ;
}
$classname = ‘MyClass‘ ;
echo $classname :: CONST_VALUE ; // 自 PHP 5.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::
<?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 ();
?>
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
<?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 ();
?>
Scope resolution operator (::)