Understanding self:: vs static:: in PHP: What’s the Difference?

PHP provides two constructs to refer to classes and their properties and methods: self:: and static::.

While these constructs may seem interchangeable, they have a key difference that can affect their behavior in certain situations. In this article, we’ll explore the difference between self:: and static:: and when to use each one.

Key takeaways

  • self:: always refers to the class in which it is used, while static:: refers to the class that is actually called at runtime.
  • self:: does not allow for late static binding, while static:: does.
  • When a child class extends a parent class that uses self::, the child class will still use the parent class’s version of the property or method. When a child class extends a parent class that uses static::, the child class will use its own version of the property or method if it overrides it.

What is self::?

self:: is a keyword in PHP that refers to the class in which it is used.

When self:: is used to access a class property or method, it always refers to the version of the property or method that is defined in the class in which it is used, regardless of whether the code is executed in a parent or child class.

Here’s an example:

class ParentClass {
    protected static $property = 'parent';

    public static function getProperty() {
        return self::$property;
    }
}

class ChildClass extends ParentClass {
    protected static $property = 'child';
}

echo ParentClass::getProperty(); // Outputs "parent"
echo ChildClass::getProperty(); // Also outputs "parent", not "child"

In this example, ParentClass defines a static property $property and a static method getProperty() that refers to $property using self::. ChildClass extends ParentClass and defines its own version of $property.

When getProperty() is called on ParentClass, it returns the value of $property in ParentClass.

However, when getProperty() is called on ChildClass, it still returns the value of $property in ParentClass, because self:: always refers to the class in which it is used (ParentClass).

What is static::?

static:: is a keyword in PHP that refers to the class that is actually called at runtime.

When static:: is used to access a class property or method, it refers to the version of the property or method that is defined in the class that is actually called, rather than the version defined in the class in which static:: is used.

Here’s an example:

class ParentClass {
    protected static $property = 'parent';

    public static function getProperty() {
        return static::$property;
    }
}

class ChildClass extends ParentClass {
    protected static $property = 'child';
}

echo ParentClass::getProperty(); // Outputs "parent"
echo ChildClass::getProperty(); // Outputs "child"

In this example, ParentClass defines a static property $property and a static method getProperty() that refers to $property using static::. ChildClass extends ParentClass and defines its own version of $property.

When getProperty() is called on ParentClass, it returns the value of $property in ParentClass. However, when getProperty() is called on ChildClass, it returns the value of $property in ChildClass, because static:: refers to the class that is actually called