The double colon (::) in PHP is one of those symbols that looks mysterious the first time you see it. Many developers understand $object->method() quickly, but ClassName::method() can make them stop and wonder, “Why is there no object here?”
The PHP scope resolution operator (::), also called the double colon operator, is used to access class-level members such as constants, static properties, and static methods. It is also used with special keywords like self, parent, and static inside class definitions.
This operator is mainly used in object-oriented PHP when you need to work with a class itself rather than an instance created from that class.
What is the PHP Scope Resolution Operator?
The PHP scope resolution operator (::), also called the double colon operator, is used to access members that belong to a class scope. Unlike the object operator (->), it does not require creating an object first when accessing static members. PHP officially documents this syntax as the scope resolution operator.
The basic syntax is:
<?php
ClassName::memberName;
?>
For example, a class constant can be accessed using the class name followed by the scope resolution operator.
<?php
class Website
{
const NAME = "PHPpot";
}
echo Website::NAME;
?>
Output:
PHPpot
Here, NAME is a class constant, so it is accessed using Website::NAME.
Scope Resolution Operator vs Object Operator
A common confusion is the difference between :: and ->. They look similar in purpose, but they work with different types of class members.
| Operator | Used for | Example |
|---|---|---|
:: |
Class-level access (static members and constants) | ClassName::method() |
-> |
Object instance access | $object->method() |
Consider this example:
<?php
class User
{
public static $count = 10;
public function showName()
{
return "John";
}
}
echo User::$count;
$user = new User();
echo $user->showName();
?>
The static property $count belongs to the class, so it is accessed using User::$count. The showName() method is an instance method, so it requires an object and uses ->.
Accessing Static Properties Using Scope Resolution Operator
A static property is shared across all objects of a class. Since it belongs to the class itself, it is accessed using the scope resolution operator.
<?php
class Counter
{
public static $value = 0;
public static function increment()
{
self::$value++;
}
}
Counter::increment();
echo Counter::$value;
?>
Output:
1
Notice the use of self::$value inside the class. The self keyword refers to the current class and is commonly used when accessing static properties or constants from within the same class.
Calling Static Methods Using the Scope Resolution Operator
Static methods can be called directly using the class name and the scope resolution operator. Since static methods do not depend on object properties, creating an object is unnecessary.
Static methods are a part of PHP’s object-oriented programming features. They are useful when a method does not depend on object data and can operate at the class level.
<?php
class Calculator
{
public static function add($number1, $number2)
{
return $number1 + $number2;
}
}
$result = Calculator::add(10, 20);
echo $result;
?>
Output:
30
This style is common for utility classes where methods perform a specific operation without maintaining object state.
For example, PHP itself uses this pattern in many places. Built-in classes such as DateTime and other standard library classes expose static methods that can be called without creating an object in certain situations.
Using self:: Inside a PHP Class
The self:: syntax is used to access members of the current class from inside the class definition. It is commonly used in PHP object-oriented programming when working with PHP classes and objects.
It is commonly used with:
- Static properties
- Static methods
- Class constants
Example:
<?php
class Configuration
{
const VERSION = "2.0";
public static function getVersion()
{
return self::VERSION;
}
}
echo Configuration::getVersion();
?>
Output:
2.0
Here, self::VERSION refers to the constant declared inside the same class.
Using parent:: to Access Parent Class Members
The parent:: keyword is used inside a child class to access methods or properties from its parent class.
The parent:: keyword is commonly used when working with PHP inheritance, where a child class extends and reuses functionality from a parent class.
This is especially useful when a child class overrides a method but still needs to execute the original parent implementation.
<?php
class Vehicle
{
public function start()
{
echo "Vehicle started";
}
}
class Car extends Vehicle
{
public function start()
{
parent::start();
echo " and car is ready";
}
}
$car = new Car();
$car->start();
?>
Output:
Vehicle started and car is ready
Without parent::start(), the overridden method in the child class would completely replace the parent method.
Using static:: for Late Static Binding
The static:: syntax is used for late static binding in PHP. It allows PHP to determine the called class at runtime instead of fixing the reference to the class where the method was originally defined. This behaviour is explained in detail in the PHP documentation for late static bindings.
This difference is important when working with inheritance.
<?php
class Product
{
public static function getName()
{
echo static::NAME;
}
}
class Book extends Product
{
const NAME = "Book";
}
Book::getName();
?>
Output:
Book
The static::NAME reference uses the class that called the method. In this example, that class is Book.
This is different from self::, which always refers to the class where the method was originally declared.
Difference Between self:: and static:: in PHP
Both self:: and static:: are used to access class members, but they behave differently when inheritance is involved.
The easiest way to remember the difference is:
self::refers to the class where the method is written.static::refers to the class that called the method.
Consider this example:
<?php
class ParentClass
{
const NAME = "Parent";
public static function showSelf()
{
echo self::NAME;
}
public static function showStatic()
{
echo static::NAME;
}
}
class ChildClass extends ParentClass
{
const NAME = "Child";
}
ChildClass::showSelf();
echo "<br>";
ChildClass::showStatic();
?>
Output:
Parent
Child
The first method uses self::, so PHP uses the constant from ParentClass. The second method uses static::, so PHP uses the constant from the class that called it, which is ChildClass.
For simple classes, self:: is usually enough. Use static:: when you are designing reusable parent classes that are expected to be extended.
Accessing Class Constants with Scope Resolution Operator
Class constants are one of the most common uses of the scope resolution operator. They provide a way to define fixed values that belong to a class instead of individual objects.
<?php
class Database
{
const HOST = "localhost";
const PORT = 3306;
}
echo Database::HOST;
echo Database::PORT;
?>
Output:
localhost3306
A class constant cannot be changed after it is declared. This makes constants useful for values such as configuration names, status codes, fixed options, and application-wide settings.
Common Mistakes When Using PHP Scope Resolution Operator
1. Using :: with Non-static Properties
The scope resolution operator cannot be used to access normal instance properties.
Incorrect:
<?php
class User
{
public $name = "John";
}
echo User::$name;
?>
This causes an error because $name is an object property, not a static property.
The correct approach is:
<?php
class User
{
public $name = "John";
}
$user = new User();
echo $user->name;
?>
2. Calling Non-static Methods Statically
A common mistake is calling a normal method using the class name.
Incorrect:
<?php
class Report
{
public function generate()
{
echo "Report generated";
}
}
Report::generate();
?>
The method should be called using an object:
<?php
$report = new Report();
$report->generate();
?>
A static call to a non-static method may produce errors or warnings depending on the PHP version and configuration. Keeping static and instance usage separate avoids unexpected behaviour.
PHP Scope Resolution Operator in Inheritance
The scope resolution operator becomes especially useful when working with class inheritance. It allows child classes to access parent functionality and control how inherited members are resolved.
The three important forms are:
| Syntax | Purpose |
|---|---|
self:: |
Access members from the current class |
parent:: |
Access members from the parent class |
static:: |
Use late static binding and resolve at runtime |
When Should You Use the Scope Resolution Operator?
The scope resolution operator is useful when you need to work with class-level functionality instead of object-specific data.
Common use cases include:
- Accessing class constants.
- Calling static methods.
- Reading or updating static properties.
- Calling parent class methods using
parent::. - Building reusable classes that use late static binding with
static::.
However, not every method should become static. A common mistake is converting methods into static methods just to avoid creating objects. If a method needs object state or depends on instance properties, an object-oriented approach using -> is usually the better design.
Practical Example: Using Scope Resolution in a Simple PHP Class
The following example combines constants, static properties, and static methods in a small class.
<?php
class Order
{
const STATUS_PENDING = "Pending";
private static int $totalOrders = 0;
public static function create()
{
self::$totalOrders++;
return "Order created with status: " . self::STATUS_PENDING;
}
public static function getTotalOrders()
{
return self::$totalOrders;
}
}
echo Order::create();
echo "<br>";
echo "Total orders: " . Order::getTotalOrders();
?>
Output:
Order created with status: Pending
Total orders: 1
In this example:
Order::create()calls a static method.Order::STATUS_PENDINGaccesses a class constant.Order::$totalOrdersaccesses a static property.self::is used inside the class to refer to its own members.
PHP Scope Resolution Operator: Quick Reference
| Syntax | Description | Example |
|---|---|---|
ClassName::constant |
Access a class constant | User::ROLE_ADMIN |
ClassName::$property |
Access a static property | Counter::$count |
ClassName::method() |
Call a static method | Helper::format() |
self:: |
Access current class members | self::CONFIG |
parent:: |
Access parent class members | parent::save() |
static:: |
Late static binding | static::TYPE |
Frequently Asked Questions
What is the scope resolution operator in PHP?
The PHP scope resolution operator (::) is used to access class constants, static properties, and static methods. It is also used with self, parent, and static inside classes.
Why is it called the scope resolution operator?
It is called the scope resolution operator because it tells PHP which class scope should be used when accessing a member. For example, ParentClass::method() explicitly tells PHP to use the method from a specific class.
Can I use :: instead of -> in PHP?
No. The two operators serve different purposes. Use :: for class-level members such as static properties and constants. Use -> for accessing properties and methods of an object instance.
What is the difference between self:: and parent::?
self:: accesses members from the current class, while parent:: accesses members from the parent class.
What is the difference between self:: and static:: in PHP?
self:: resolves to the class where the method is declared. static:: uses late static binding and resolves to the class that called the method.
Conclusion
The PHP scope resolution operator (::) is mainly used when working with class-level features. It provides a clean way to access constants, static properties, and static methods without creating objects.
The important distinction to remember is simple: use :: when working with the class itself, and use -> when working with an object created from that class.
Once you understand the difference between self::, parent::, and static::, many advanced PHP object-oriented patterns become much easier to read and maintain.
Excellent writeup. Its nice information. Thanks.
Welcome Joe
Very nice explanations regarding (:: ) I real accept the tutorial.
Thank you Simon
nice explanations
Thank you Baghel