Scope resolution operator: Difference between revisions
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
|||
Line 5: | Line 5: | ||
== C++ == |
== C++ == |
||
< |
<syntaxhighlight lang="cpp"> |
||
class A { |
class A { |
||
public: |
public: |
||
Line 17: | Line 17: | ||
int A::i = 4; // scope operator refers to the integer i declared in the class A |
int A::i = 4; // scope operator refers to the integer i declared in the class A |
||
int x = B::j; // scope operator refers to the integer j declared in the namespace B |
int x = B::j; // scope operator refers to the integer j declared in the namespace B |
||
</syntaxhighlight> |
|||
</source> |
|||
== PHP == |
== PHP == |
||
Line 28: | Line 28: | ||
| accessdate = 2007-08-09 |
| accessdate = 2007-08-09 |
||
}}</ref> [[Zend Engine]] 0.5 used in [[PHP#PHP_3_and_4|PHP 3]]. Although it has been confusing to many developers who do not speak Hebrew, it is still being used in PHP 7, as in this sample error message: |
}}</ref> [[Zend Engine]] 0.5 used in [[PHP#PHP_3_and_4|PHP 3]]. Although it has been confusing to many developers who do not speak Hebrew, it is still being used in PHP 7, as in this sample error message: |
||
< |
<syntaxhighlight lang="console"> |
||
$ php -r :: |
$ php -r :: |
||
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM |
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM |
||
</syntaxhighlight> |
|||
</source> |
|||
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: |
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: |
||
< |
<syntaxhighlight lang="console"> |
||
$ php -r 'define("foo", "bar"); if (empty(foo)) echo "empty";' |
$ php -r 'define("foo", "bar"); if (empty(foo)) echo "empty";' |
||
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM |
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM |
||
</syntaxhighlight> |
|||
</source> |
|||
As of PHP 5.4, error messages concerning the scope resolution operator still include this name, but have clarified its meaning somewhat: |
As of PHP 5.4, error messages concerning the scope resolution operator still include this name, but have clarified its meaning somewhat: |
||
< |
<syntaxhighlight lang="console"> |
||
$ php -r :: |
$ php -r :: |
||
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) |
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) |
||
</syntaxhighlight> |
|||
</source> |
|||
There are other less obvious ways to trigger the error, for example by attempting to use the following invalid PHP expression: |
There are other less obvious ways to trigger the error, for example by attempting to use the following invalid PHP expression: |
||
< |
<syntaxhighlight lang="console"> |
||
$ php -r static const $a=1 |
$ php -r static const $a=1 |
||
Parse error: syntax error, unexpected end of file, expecting :: (T_PAAMAYIM_NEKUDOTAYIM) |
Parse error: syntax error, unexpected end of file, expecting :: (T_PAAMAYIM_NEKUDOTAYIM) |
||
</syntaxhighlight> |
|||
</source> |
|||
== Ruby == |
== Ruby == |
||
In [[Ruby (programming language)|Ruby]], scope resolution can be specified using [[namespaces]] (such as classes or modules). |
In [[Ruby (programming language)|Ruby]], scope resolution can be specified using [[namespaces]] (such as classes or modules). |
||
< |
<syntaxhighlight lang="ruby"> |
||
module Example |
module Example |
||
Version = 1.0 |
Version = 1.0 |
||
Line 74: | Line 74: | ||
# We can't do the same with ::Version and .Version, because Version is within the scope of Example, but |
# 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. |
# Example can't respond to the message Version, since there is no method to respond with. |
||
</syntaxhighlight> |
|||
</source> |
|||
Scope is also affected by [[Sigil (computer programming)|sigils]] which preface variable names: |
Scope is also affected by [[Sigil (computer programming)|sigils]] which preface variable names: |
Revision as of 18:30, 11 May 2020
This article needs additional citations for verification. (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. The specific uses vary across different programming languages with the notions of scoping. In many languages the scope resolution operator is written "::".
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 .
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++
class A {
public:
static int i; // scope of A
};
namespace B {
int j = 2;
} // namespace B
int A::i = 4; // scope operator refers to the integer i declared in the class A
int x = B::j; // scope operator refers to the integer j declared in the namespace B
PHP
In PHP, the scope resolution operator is also called Paamayim Nekudotayim (Template:Lang-he, pronounced [paʔaˈmajim nekudoˈtajim], the second word a colloquial corruption of נקודתיים, pronounced [nekudoˈtajim]), which means “double colon” in Hebrew.
The name "Paamayim Nekudotayim" was introduced in the Israeli-developed[1] Zend Engine 0.5 used in PHP 3. Although it has been confusing to many developers who do not speak Hebrew, it is still being used in PHP 7, 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
As of PHP 5.4, error messages concerning the scope resolution operator still include this name, but have clarified its meaning somewhat:
$ php -r ::
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
There are other less obvious ways to trigger the error, for example by attempting to use the following invalid PHP expression:
$ php -r static const $a=1
Parse error: syntax error, unexpected end of file, expecting :: (T_PAAMAYIM_NEKUDOTAYIM)
Ruby
In Ruby, scope resolution can be specified using namespaces (such as classes or modules).
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.
Scope is also affected by sigils which preface variable names:
- "
$
" - global variable - "
@
" - instance variable ofself
- "
@@
" - class variable - No sigil, lowercase or underscore - local variable or method
- No sigil, uppercase - constant
References
- ^ "Scope Resolution Operator". PHP 5 Manual. Retrieved 2007-08-09.