PHP Scope Resolution Operator

Why this is called as scope resolution operator? This operator is used to refer the scope of some block or program context like classes, objects, namespace and etc. For this reference an identifier is used with this operator to access or reproduce the code inside that scope.

For example, in PHP, the scope resolution operators are used to access the properties and methods of classes.

Accessing PHP Class Variables and Functions using Scope Resolution Operator

We have to use class name to call the variables and functions of the class. For example,

class Organisation {
function getStrength() {
return $strength;
} 
}
$strength = Organisation::getStrength();

This operator is used when no object has been created till the class functions or variables are accessed from outside the scope of the class. Otherwise, it can be called by using class objects like as shown below.

class Organisation {
function getStrength() {
return $strength;
} 
}
$objOrganisation = new Organisation();
$strength = $objOrganisation->getStrength();

As of PHP 5.3.0, the class name can be stored into a PHP variable using which the class properties are called with scope resolution operator. For example,

class Organisation {
function getStrength() {
return $strength;
} 
}
$varOrganisation = "Organisation";
$strength = $varOrganisation::getStrength();

Not only the names of PHP classes and objects that are associated with the scope resolution operator to access class properties and functions. But some set of keywords like, self, static, parent are used for this purpose. But, these keywords are used inside class definition.

php_scope_resolution_operator

This operator is known as Paamayim Nekudotayim named in Hebrew means double colon. In PHP, the errors that occurred related to this scope resolution operator will be displayed to the browser using this name only, that is T_PAAMAYIM_NEKUDOTAYIM, a PHP error constant that denote improper code lines in this regard.

For example, if we use the scope resolution operators unnecessarily, then the following error will be return to the browser.

Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in ... on line...

Or otherwise, if we ignore to use this operator, though it is requires, then the error notice will be,

Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in  ... on line...

This PHP code tutorial was published on June 10, 2013.

↑ Back to Top