
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Scope Resolution Operator
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
"; } } 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
Advertisements