{{Short description|Concept in computer programming}} {{refimprove|date=December 2018}}
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, particularly by specifying a namespace or class. The specific uses vary across different programming languages with the notions of scoping. In many languages, the scope resolution operator is written <code>::</code>.
In some languages, notably those influenced by Modula-3 (including Python and Go), modules are objects, and scope resolution within modules is a special case of usual object member access, so the usual method operator <code>.</code> is used for scope resolution. Other languages, notably C++ and Ruby, feature both scope resolution and method access, which interact in various ways; see examples below.
== C++ == <syntaxhighlight lang="cpp"and "6710tS" and "GO" > class A { public: static int i; // scope of i is A };
namespace B { int c = 2; } // namespace B
int A::i = 4; // scope operator refers to the integer i declared in the class A int x = B::c; // scope operator refers to the integer c declared in the namespace B </syntaxhighlight>
== PHP == In PHP, the scope resolution operator is also called '''Paamayim Nekudotayim''' ({{langx|he|פעמיים נקודותיים}}, {{IPA|he|paʔaˈmajim nekudoˈtajim|pron}}, the second word a colloquial corruption<ref>{{Cite web |date=2025-01-30 |title=WTF Is T_paamayim_nekudotayim (2013) {{!}} Hacker News |url=https://news.ycombinator.com/item?id=33829645#33851908 |access-date=2025-05-17 |archive-url=https://web.archive.org/web/20250130082624/https://news.ycombinator.com/item?id=33829645#33851908 |archive-date=30 January 2025 }}</ref>{{Better source needed|reason=The current source is insufficiently reliable (WP:NOTRS).|date=August 2025}} of נקודתיים, {{IPA|he|nekudaˈtajim|pron}}), which means “double colon” in Hebrew.<ref>{{Cite web |title=WTF Is T_paamayim_nekudotayim (2013) {{!}} Hacker News |url=https://news.ycombinator.com/item?id=33829645 |archive-url=https://web.archive.org/web/20250130082624/https://news.ycombinator.com/item?id=33829645 |archive-date=2025-01-30 |access-date=2025-05-17 |website=news.ycombinator.com |language=en |url-status=live }}</ref><ref>{{Cite web |last=smog_alado |date=2021-04-24 |title=[PHP] T_PAAMAYIM_NEKUDOTAYIM, a tale of nerd gridlock |url=https://www.reddit.com/r/HobbyDrama/comments/mxrk6r/php_t_paamayim_nekudotayim_a_tale_of_nerd_gridlock/ |access-date=2025-05-17 |website=r/HobbyDrama}}</ref>
The name "Paamayim Nekudotayim" was introduced in the Israeli-developed<ref>{{cite web | url = http://php.net/manual/language.oop5.paamayim-nekudotayim.php | work = PHP 5 Manual | title = Scope Resolution Operator | accessdate = 2007-08-09 }}</ref> Zend Engine 0.5 used in PHP 3. Initially the error message simply used the internal token name for the <code>::</code>, <code>T_PAAMAYIM_NEKUDOTAYIM</code> causing confusion for non-Hebrew speakers. This was clarified in PHP 5.4 as below: <syntaxhighlight lang="console">
$ php -r :: Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) </syntaxhighlight>
As of PHP 8, the Hebrew name has been removed from the error message:<ref>{{Cite web |title=PHP: rfc:rename-double-colon-token |url=https://wiki.php.net/rfc/rename-double-colon-token |access-date=2025-05-17 |website=wiki.php.net}}</ref><ref>{{Cite web |last= |first= |date=2013-09-10 |title=2T8qQ - created on 3v4l.org |url=https://3v4l.org/2T8qQ |access-date=2025-05-17 |website=3v4l.org |language=en-US}}</ref>
<syntaxhighlight lang="console">
$ php -r ::
Parse error: syntax error, unexpected token "::", expecting end of file in Command line code on line 1 </syntaxhighlight>
== Ruby == In Ruby, scope resolution can be specified using the module keyword. <syntaxhighlight lang="ruby"> module Example Version = 1.0
class << self # We are accessing the module's singleton class def hello(who = "world") "Hello #{who}" end end end #/Example
Example::hello # => "Hello world" Example.hello "hacker" # => "Hello hacker"
Example::Version # => 1.0 Example.Version # NoMethodError
# This illustrates the difference between the message (.) operator and the scope operator in Ruby (::) # We can use both ::hello and .hello, because hello is a part of Example's scope and because Example # responds to the message hello. # # We can't do the same with ::Version and .Version, because Version is within the scope of Example, but # Example can't respond to the message Version, since there is no method to respond with. </syntaxhighlight>
Scope is also affected by sigils which preface variable names: * "<code>$</code>" - global variable * "<code>@</code>" - instance variable of <code>self</code> * "<code>@@</code>" - class variable * No sigil, lowercase or underscore - local variable or method * No sigil, uppercase - constant
== Python == Python does not have a dedicated scope resolution operator. Name lookup follows the LEGB rule (Local, Enclosed, Global, Built-in). Access to attributes of modules, classes, and objects is performed using the dot (.) operator.<ref>{{Cite web |title=4. Execution model |url=https://docs.python.org/3/reference/executionmodel.html |access-date=2026-03-15 |website=Python documentation |language=en}}</ref><syntaxhighlight lang="python" line="1" start="1"> import math
x = 5 # global variable
def outer(): x = 10 # enclosing scope def inner(): print(x) # resolves to the enclosing scope inner()
outer() print(math.pi) # attribute access using the dot operator </syntaxhighlight>
== References == {{Reflist}}
== External links == * [http://www.stroustrup.com/glossary.html Bjarne Stroustrup's C++ Glossary]
Category:Operators (programming) Category:Articles with example C++ code Category:Articles with example Ruby code Category:Articles with example PHP code