PHP Scope Resolution Operator

PHP Scope Resolution Operator

Why is this 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 the 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 likes, self, static, parent are used for this purpose. But, these keywords are used inside the class definition.

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 in the browser using this name only, that is T_PAAMAYIM_NEKUDOTAYIM, a PHP error constant that denotes improper code lines in this regard.
For example, if we use the scope resolution operators unnecessarily, then the following error will be a 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 required, 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.

Previous:MySQL Right JOINNext:PHP Variable Varibles
↑ Back to Top

PHPPHP IntroductionPHP BasicsPHP Shopping CartPHP Contact FormTypes, Variables & OperatorsPHP Data TypesPHP Resource Data TypePHP Data Type ConversionPHP Type JugglingPHP Type HintingPHP VariablesPHP Variable VariblesPHP Variable InterpolationPHP Variable ScopePHP Globals with EGPCS InformationPHP $_SERVER VariablePHP OperatorsPHP Scope Resolution OperatorPHP StringsPHP ArraysPHP FunctionsPHP OOPSPHP FormsAdvancedPHP AJAXRESTful APIPHP DatabasesPHP Sessions and CookiesError and Exception HandlingFiles and DirectoriesPHP Date TimePHP GraphicsPHP XMLPHP Code SamplesPHP FreelancerjQueryWordpressMySQLCSS

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons
Social Media Auto Publish Powered By : XYZScripts.com