Back to Blog Page
Backend Development

What is Double Colon in PHP? PHP Scope Resolution Operator Explained

What is Double Colon in PHP? PHP Scope Resolution Operator Explained

Written by Kolade Chris | Aug 3, 2024 | #PHP | 2 minute Read

The double colon (::) is the scope resolution operator in PHP. It is also known as Paamayim Nekudotayim, which means double colon in Hebrew.

The scope resolution operator is used to access static methods, static properties, and constants of a class without creating an instance of that class. It allows you to access these elements directly from the class itself, rather than from an object instance.

How to Access Static Methods with the Scope Resolution Operator

To use the scope resolution operator to access static methods, attach the double colon to the class name, then call the static method like a regular function:

1
ClassName::methodName()

The example below accesses the numberOfLegs method from the Animal class without instantiating it:

1
class Animal
2
{
3
public static function numberOfLegs($animal)
4
{
5
switch ($animal) {
6
case 'dog':
7
case 'cat':
8
return 4;
9
break;
10
case 'bird':
11
return 2;
12
break;
13
default:
14
return 'Unknown';
15
}
16
}
17
}
18
19
// Accessing the static method directly without creating an instance
20
echo "A dog has " . Animal::numberOfLegs('dog') . " legs.<br>";
21
// Output: A dog has 4 legs.
22
echo "A bird has " . Animal::numberOfLegs('bird') . " legs.<br>";
23
// Output: A bird has 2 legs.
24
echo "A fish has " . Animal::numberOfLegs('fish') . " legs.<br>";
25
// Output: A fish has Unknown legs.

If you want to instantiate the class before accessing the numberOfLegs method, this is how you would have done it:

1
class Animal {
2
public static function numberOfLegs($animal) {
3
switch ($animal) {
4
case 'dog':
5
case 'cat':
6
return 4;
7
case 'bird':
8
return 2;
9
default:
10
return 'Unknown';
11
}
12
}
13
}
14
15
// Instantiate the Animal class
16
$animalInstance = new Animal();
17
18
// Access the static method using the object instance
19
echo "A dog has " . $animalInstance->numberOfLegs('dog') . " legs.<br>";
20
// Output: A dog has 4 legs.
21
22
echo "A bird has " . $animalInstance->numberOfLegs('bird') . " legs.<br>";
23
// Output: A bird has 2 legs.
24
25
echo "A fish has " . $animalInstance->numberOfLegs('fish') . " legs.<br>";
26
// Output: A fish has Unknown legs.

Subscribe to my newsletter for coding tips, videos from reputable sources, articles from OG tech authors, and a ton of other goodies.

No BS. No fluff. Just pure software development goodies on a Sunday every week.

How to Access Static Properties with the Scope Resolution Operator

Accessing static properties with the scope resolution is done the same way you access static methods with it.

That means you need to attach the double colon to the class name, and then the property name. The only thing you need to do differently is to attach the dollar sign to the property name:

1
ClassName::$propertyName

In the example below, I accessed the $numberOfLegs static property without instantiating the Animal2 class:

1
class Animal2
2
{
3
public static $numberOfLegs = 4;
4
}
5
6
echo "The number of legs for a tetrapoda animal is " . Animal2::$numberOfLegs;
7
// Output: The number of legs for a tetrapoda animal is 4

How to Access Constants with the Scope Resolution Operator

To access constants from a class with the scope resolution operator, you do it like below:

1
ClassName::CONSTANT_NAME

Here’s an example:

1
class Animal3
2
{
3
const LEGS = 4;
4
}
5
6
echo "The number of legs of a tetrapoda animal is " . Animal3::LEGS;
7
// Output: The number of legs of a tetrapoda animal is 4

Wrapping Up

The scope resolution operator (::) does not just allow access to static properties, static methods, and constants within a class without the need to instantiate the class. It provides a convenient way to interact with class-level elements directly, simplifying code and improving efficiency in the process.