- Scope resolution operator
-
In computer programming, scope is an enclosing context where values and expressions are associated. The scope resolution operator helps to identify and specify the context to which an identifier refers. The specific uses vary across different programming languages with the notions of scoping.
Contents
C++
The scope resolution operator (::) in C++ is used to define the already declared member functions (in the header file with the .hpp or the .h extension) of a particular class. In the .cpp file one can define the usual global functions or the member functions of the class. To differentiate between the normal functions and the member functions of the class, one needs to use the scope resolution operator (::) in between the class name and the member function name i.e. ship::foo() where ship is a class and foo() is a member function of the class ship. The other uses of the resolution operator is to resolve the scope of a variable when the same identifier is used to represent a global variable, a local variable, and members of one or more class(es). If the resolution operator is placed between the class name and the data member belonging to the class then the data name belonging to the particular class is referenced. If the resolution operator is placed in front of the variable name then the global variable is referenced. When no resolution operator is placed then the local variable is referenced.
Example
#include <iostream> // Without this using statement cout below would need to be std::cout using namespace std; int n = 12; // A global variable int main() { int n = 13; // A local variable cout << ::n << endl; // Print the global variable: 12 cout << n << endl; // Print the local variable: 13 }
PHP
In PHP, the scope resolution operator is also called Paamayim Nekudotayim (Hebrew: פעמיים נקודתיים, pronounced [paʔaˈmajim nəkudoˈtajim]), which means "twice colon" or "double colon" in Hebrew.
The name was introduced in the Israeli-developed[1] Zend Engine 0.5 used in PHP 3. Although it has been confusing to many developers who don't speak Hebrew, it is still being used in PHP 5, as in this sample error message:
$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
A similar error can also occur where no scope resolution operator is present. For example, attempting to check whether a constant is empty() triggers this error:
$ php -r "define('foo', 'bar'); if (empty(foo)) echo 'empty';"
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM
References
- ^ "Scope Resolution Operator". PHP5 Manual. http://php.net/manual/language.oop5.paamayim-nekudotayim.php. Retrieved 2007-08-09.
Categories:- PHP programming language
Wikimedia Foundation. 2010.