Scope resolution operator

From Wikipedia, the free encyclopedia

  (Redirected from Paamayim Nekudotayim)
Jump to: navigation, search

In computer programming, scope is an enclosing context where values and expressions are associated. For example, if a function references a variable 'a', what value is used? A variable defined in that function? A variable defined in the function that called that function? A variable in the same namespace?

Various programming languages have various types of scopes.

Some programming languages have an operator that allows a programmer to specify how a particular variable should be found.

Within this general definition, the types of scope resolution operators that exist, what they signify, and how they work varied widely across programming languages.

Contents

[edit] Ruby

Ruby has:

  • local variables (e.g. only_see_me_inside_the_same_function)
  • instance variables (e.g. @just_for_the_owning_object)
  • class variables (e.g. @@all_objects_of_this_type_share_this)
  • global variables (e.g. $the_entire_program_shares_this)

[edit] C++

The scope resolution operator (::) in C++ is used to define the already declared member functions (in the header file with the .hpp extension) of the class. In the .cpp file one can define the normal functions or the member functions of the class. To differentiate from the normal functions with 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 the ship is the class and the foo() is the member function in the ship. The other uses of the resolution operator is to resolve the scope of the variables if the same variable name is used for the global, local, and the data member of the class. 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 affected. If the resolution operator is placed in front of the variable name then the global variable is affected. If no resolution operator is placed then the local variable is affected.

[edit] Example

#include <iostream>

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
         << n   << endl;  // Print the local variable: 13
}

[edit] PHP

In PHP scope resolution operator is also called Paamayim Nekudotayim (Hebrew: פעמיים נקודתיים‎, pronounced [paʔamajim nəkudotajim])[where's the stress?], which means "twice colon" or "double colon" in Hebrew. It may be helpful to note that in some cases this error may occur when the $ symbol is not included in the use of a variable.

Nekudotayim (נקודתיים) means 'colon'; it comes from nekuda [nəkuda],[where's the stress?] 'point' or 'dot', and the dual suffix ayim (יים-), hence 'two points'. Similarly, the word paamayim (פעמיים) is derived by attaching the dual suffix to paam [paʔam] ('one time' or 'once'), thus yielding 'twice'.

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, it is still being used in PHP 5, as in this sample error message:

$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in Command line code on line 1

This 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 in Command line code on line 1

[edit] See also

[edit] References

Personal tools