Home » Php » What do two colons mean in PHP?

What do two colons mean in PHP?

Posted by: admin November 29, 2017 Leave a comment

Questions:

I don’t know what it’s doing when we have this situation:

Foo::Bar

It looks like a path.

Answers:

That’s (generally) for accessing a static method or property in a class. It’s called the scope resolution operator, or Paamayim Nekudotayim (which leads to some amazingly confusing error messages!). See http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php.

Questions:
Answers:

The :: operator is the scope resolution operator. It is used to access class constants or static properties and methods, either from outside the class:

ClassName::CONSTANT_VALUE
ClassName::staticMethod()

Or within a class method to reference the same or a parent class using self and parent:

self::CONSTANT_VALUE
self::staticMethod()
parent::CONSTANT_VALUE
parent::staticMethod()

Questions:
Answers:

The Scope Resolution Operator(::)the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

<?php
class A {

public static $B = '1'; # Static class variable.

const B = '2'; # Class constant.

public static function B() { # Static class function.
    return '3';
}

}

echo A::$B . A::B . A::B(); # Outputs: 123
?>