Current Location: Home> Latest Articles> PHP self Keyword Explained: Using Static Members, Methods, and Constants

PHP self Keyword Explained: Using Static Members, Methods, and Constants

gitbox 2025-07-14

PHP self Keyword Explained: Using Static Members, Methods, and Constants

In PHP, self is a special keyword used to refer to the current class. It is commonly used within a class to reference static member variables, static methods, and constants. Understanding how to use self within a class can help developers work with static content more effectively.

Using self to Reference Static Member Variables

The self keyword allows you to reference static member variables of the current class. Static member variables belong to the class itself, not to instances of the class, and are shared by all instances.

Here's an example:

class MyClass {
    public static $count = 0;
    public function __construct() {
        self::$count++;
    }
    public function getCount() {
        return self::$count;
    }
}
$obj1 = new MyClass();
$obj2 = new MyClass();
echo $obj1->getCount(); // Outputs 2
echo $obj2->getCount(); // Outputs 2

In this example, self::$count is used to reference the static member variable $count of the MyClass class, and its value is incremented in the constructor. Regardless of how many objects are created, the value of $count remains consistent across all instances.

Using self to Reference Static Methods

In addition to static member variables, self can also be used to reference static methods of the current class. Static methods are special methods within a class that can be called directly without instantiating the class.

Here's an example:

class MathUtils {
    public static function add($a, $b) {
        return $a + $b;
    }
}
class MyClass {
    public function calculate() {
        return self::add(2, 3);
    }
}
$obj = new MyClass();
echo $obj->calculate(); // Outputs 5

In this example, the calculate method of the MyClass class calls the static MathUtils::add method. By using self::add, the static method is referenced directly without needing to instantiate the MathUtils class.

Using self to Reference Class Constants

In addition to static member variables and static methods, self can be used to reference constants within the current class. Constants are immutable values defined within a class.

Here's an example:

class MathUtils {
    const PI = 3.14;
}
class Circle {
    public function getArea($radius) {
        return self::PI * $radius * $radius;
    }
}
$obj = new Circle();
echo $obj->getArea(2); // Outputs 12.56

In this example, the Circle class references the constant PI from the MathUtils class using self::PI to calculate the area of a circle.

Summary

In PHP, the self keyword refers to the current class and can be used to reference static member variables, static methods, and constants. It allows developers to easily access and manipulate static content within a class, making it an essential feature of PHP programming.