PHP Late Static Bindings

Late static binding in PHP allows you to reference the called class in a static inheritance context rather than the class where the method is defined. When using static:: instead of self::, PHP resolves the class name at runtime based on the calling context.

The Problem with self::

When static methods use self::, the class reference is resolved at compile time to the class where the method is defined ?

<?php
class test1 {
    public static function name() {
        echo "name of class: " . __CLASS__;
    }
    
    public static function getname() {
        self::name(); // Always refers to test1
    }
}

class test2 extends test1 {
    public static function name() {
        echo "name of class: " . __CLASS__;
    }
}

test2::getname();
?>
name of class: test1

The output shows the parent class name because self:: was resolved to test1 at compile time.

Solution with static::

Using static:: creates late static binding, resolving the class at runtime ?

<?php
class test1 {
    public static function name() {
        echo "name of class: " . __CLASS__;
    }
    
    public static function getname() {
        static::name(); // Resolved at runtime
    }
}

class test2 extends test1 {
    public static function name() {
        echo "name of class: " . __CLASS__;
    }
}

test2::getname();
?>
name of class: test2

Late Static Binding in Non-Static Context

Late static binding also works with instance methods. However, private methods maintain their original scope ?

<?php
class test1 {
    private function name() {
        echo "name of class: " . __CLASS__ . "
"; } public function getname() { $this->name(); static::name(); // Still calls test1::name() } } class test2 extends test1 { // Inherits private name() method from test1 } $t2 = new test2(); $t2->getname(); ?>
name of class: test1
name of class: test1

Comparison

Keyword Resolution Time Refers To
self:: Compile time Class where method is defined
static:: Runtime Class that called the method

Conclusion

Late static binding with static:: enables proper inheritance behavior in static contexts, allowing child classes to override parent methods effectively. Use static:: when you want runtime class resolution.

Updated on: 2026-03-15T09:14:14+05:30

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements