Paamayim Nekudotayim In PHP

What? Don't worry, I can't say it either. It is officially called the Scope Resolution Operator (but also just a double colon) and is used to reference static properties and functions of a class. It is also used to reference overridden functions of classes.

To reference a constant of a class you do something similar to the following.

class MyClass {
 const CONST_VALUE = 'A constant value';
}
 
echo MyClass::CONST_VALUE;

To call a static function or a parameter you need to include the word static in the function or parameter definition. You can then reference this function through the scope resolution operator.

class MyClass {
 public static $my_static = 'static var';
 
 public static function thisIsFunction() {
 
 }
}
 
echo MyClass::$my_static; // prints 'static var'
MyClass::thisIsFunction(); // calls thisIsFunction() in MyClass

You can also use the scope resolution operator to reference functions and parameters in parent classes. This is accomplished by using the parent operator. The following code has two class definitions, one of which basically exists to call the function from the parent.

class MyClass {
 public static $my_static = 'static var';
 
 public static function thisIsFunction() {
 
 }
}
 
class ChildClass extends MyClass{
 
 public static function childFunction() {
  parent::thisIsFunction();
 }
}
 
OtherClass::childFunction(); // calls childFunction() in MyClass

The call to the childFunction() function basically calls the function thisIsFunction() in the parent class. This is useful if you want to override the parent function, but still use most of the basic functionality. For example, the child class could take in a parameter, which it then formats or alters and passes this to the parent class.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.