Introduction
In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator. This operator is also called Paamayim Nekudotayim, which in Hebrew means double colon.
Syntax
<?php class A{ const PI=3.142; static $x=10; } echo A::PI; echo A::$x; $var='A'; echo $var::PI; echo $var::$x; ?>
Inside class
To access class level items inside any method, keyword - self is used
<?php class A{ const PI=3.142; static $x=10; static function show(){ echo self::PI . self::$x; } } A::show(); ?>
In child class
If a parent class method overridden by a child class and you need to call the corresponding parent method, it must be prefixed with parent keyword and scope resolution operator
Example
<?php class testclass{ public function sayhello(){ echo "Hello World\n"; } } class myclass extends testclass{ public function sayhello(){ parent::sayhello(); echo "Hello PHP"; } } $obj=new myclass(); $obj->sayhello(); ?>
Output
This will produce following output −
Hello World Hello PHP