PHP Scope Resolution Operator

by Vincy. Last modified on July 3rd, 2022.

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,

<?php

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.

<?php

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,

<?php

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...
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Scope Resolution Operator”

Leave a Reply

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

↑ Back to Top

Share this page