In PHP the Scope Resolution Operator (which is actually the double colon or Paamayim Nekudotayim as it is its official name) has many uses. From the PHP manual pages (Manual) I quote
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class. When referencing these items from outside the class definition, use the name of the class.
So, I recently decided to go ahead and test this operator in a bit more detail and discovered something I did not know about the way PHP decides to give you access to its public methods.
Let us consider the following php class
class A { protected $name; public function getName() { return $this->name; } public function getHalloPublic() { return 'Public message: Hallo everybody!'; } protected function getHalloProtected() { return 'Protected Message: Hallo everybody!'; } }
This class only has one property, the name
which is protected and is populated when constructing a new object of class A
. We have also defined a public function getName()
to retrieve the name
property of the class. In addition we have defined two methods the getHalloPublic()
, which is a public method, and the getHalloProtected()
, which is a protected method.
Now let’s go ahead and test the functionality of our class and methods with regards to the Scope Resolution Operator. In order to do that we have instantiated an object of the A
class called a
. Using this object we try to access the three methods we have defined like:
$a = new A('Foo'); echo $a->getName() . PHP_EOL; echo $a->getHalloPublic() . PHP_EOL; echo $a->getHalloProtected() . PHP_EOL;
The result of the above snippet of code will be:
Foo Public message: Hallo everybody! PHP Fatal error: Call to protected method A::getHalloProtected() from context '' in ...
which is what we would expect, right? The getName()
returns the value of the name
property of our object, the getHalloPublic()
method is executed and we print the string that it returns and the getHalloProtected()
method throws a PHP fatal error since we have defined it as protected which means that we cannot call it from outside the scope of the class or the classes that extend it.
OK, so now let’s try to do things a bit differently:
echo A::getName() . PHP_EOL;
If we do not instantiate an object of the A
class but instead we try to directly access the getName()
method of the class we will get:
PHP Fatal error: Using $this when not in object context ...
which makes sence because the getName()
method requires an object so that it can make use of $this
variable.
OK, second test now:
echo A::getHalloPublic() . PHP_EOL;
What I would expect PHP to say now, is, that you cannot run this piece of code because this is not a static method that you are calling, and is not a parent method of an object that we happen to call from one of its overridden methods. But what is the outcome of this piece of code?
Public message: Hallo everybody!
How about if we run
echo A::getHalloProtected() . PHP_EOL;
The outcome would be a PHP fatal error
PHP Fatal error: Call to protected method A::getHalloProtected() from context '' in
So, what is going on? It seems that the Scope Operator allows us to (try) to run every method defined within a class. In the previous example when we run
<b>echo A::getHalloPublic() . PHP_EOL;</b>
PHP executed the code of the method as it would if it were a static method. In the example where we run
<b>echo A::getHalloProtected() . PHP_EOL;</b>
PHP tried to execute the code but because the method is defined as protected it cannot run it outside the class hierarchy.