PHP: The Scope Resolution Operator (::)

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

[cc_php tab_size=”4″ escaped =”true”]
name = $name;
}

public function getName()
{
return $this->name;
}

public function getHalloPublic()
{
return ‘Public message: Hallo everybody!’;
}

protected function getHalloProtected()
{
return ‘Protected Message: Hallo everybody!’;
}
}
[/cc_php]

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:

[cc_php tab_size=”4″ escaped=”true”]
$a = new A(‘Foo’);
echo $a->getName() . PHP_EOL;
echo $a->getHalloPublic() . PHP_EOL;
echo $a->getHalloProtected() . PHP_EOL;
[/cc_php]

The result of the above snippet of code will be:

[cc_bash escaped=”true”]
Foo
Public message: Hallo everybody!
PHP Fatal error: Call to protected method A::getHalloProtected() from context ” in …
[/cc_bash]

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:

[cc_php tab_size=”4″ escaped=”true”]
echo A::getName() . PHP_EOL;
[/cc_php]

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:

[cc_bash escaped=”true”]
PHP Fatal error: Using $this when not in object context …
[/cc_bash]

which makes sence because the

getName()

method requires an object so that it can make use of

$this

variable.
Ok, second test now:

[cc_php tab_size=”4″ escaped=”true”]
echo A::getHalloPublic() . PHP_EOL;
[/cc_php]

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?

[cc_bash escaped=”true”]
Public message: Hallo everybody!
[/cc_bash]

How about if we run

[cc_php tab_size=”4″ escaped=”true”]
echo A::getHalloProtected() . PHP_EOL;
[/cc_php]

The outcome would be a PHP fatal error

[cc_bash escaped=”true”]
PHP Fatal error: Call to protected method A::getHalloProtected() from context ” in
[/cc_bash]

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

echo A::getHalloPublic() . PHP_EOL;

PHP executed the code of the method as it would if it were a static method. In the example where we run

echo A::getHalloProtected() . PHP_EOL;

PHP tried to execute the code but because the method is defined as protected it cannot run it outside the class hierarchy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s