# Scope resolution operator

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Scope_resolution_operator
> Markdown URL: https://mediated.wiki/source/Scope_resolution_operator.md
> Source: https://en.wikipedia.org/wiki/Scope_resolution_operator
> Source revision: 1343574744
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Concept in computer programming}}
{{refimprove|date=December 2018}}

In [computer programming](/source/computer_programming), [scope](/source/Scope_(computer_science)) is an enclosing context where [values](/source/Value_(computer_science)) and [expression](/source/Expression_(computer_science))s are associated. The '''scope resolution operator''' helps to identify and specify the context to which an [identifier](/source/Identifier_(computer_languages)) refers, particularly by specifying a [namespace](/source/namespace) or [class](/source/C%2B%2B_classes). The specific uses vary across different [programming language](/source/programming_language)s 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](/source/Modula-3) (including [Python](/source/Python_(programming_language)) and [Go](/source/Go_(programming_language))), modules are [object](/source/Object_(computer_science))s, 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++](/source/C%2B%2B) and [Ruby](/source/Ruby_(programming_language)), 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](/source/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](/source/WP%3ANOTRS)).|date=August 2025}} of נקודתיים, {{IPA|he|nekudaˈtajim|pron}}), which means “double [colon](/source/colon_(punctuation))” 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&nbsp;5 Manual
  | title = Scope Resolution Operator
  | accessdate = 2007-08-09
}}</ref> [Zend Engine](/source/Zend_Engine) 0.5 used in [PHP 3](/source/PHP). 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](/source/Ruby_(programming_language)), 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](/source/Sigil_(computer_programming)) which preface variable names:
* "<code>$</code>" - [global variable](/source/global_variable)
* "<code>@</code>" - [instance variable](/source/instance_variable) of <code>[self](/source/this_(computer_programming))</code>
* "<code>@@</code>" - [class variable](/source/class_variable)
* No sigil, lowercase or underscore - [local variable](/source/local_variable) or method
* No sigil, uppercase - [constant](/source/Constant_(computer_programming))

== Python ==
[Python](/source/Python_(programming_language)) 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

---
Adapted from the Wikipedia article [Scope resolution operator](https://en.wikipedia.org/wiki/Scope_resolution_operator) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Scope_resolution_operator?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
