← Back to Chapters

PHP OOP Late Static Binding

? PHP OOP Late Static Binding

? Quick Overview

Late Static Binding (LSB) in PHP allows a class to reference the called class instead of the class where the method is defined. It solves inheritance issues when static methods need to behave differently for child classes.

? Key Concepts

  • static:: resolves at runtime based on the calling class
  • self:: resolves at compile time to the current class
  • Works only with static methods and properties
  • Essential for polymorphism in static contexts

? Syntax / Theory

When a parent class uses static::method(), PHP waits until runtime to determine which class invoked the method. This enables child classes to override static behavior while still reusing parent logic.

? Code Example

? View Code Example
// Demonstrates Late Static Binding using static::
<?php
class ParentClass {
public static function who() {
echo __CLASS__;
}

public static function test() {
static::who();
}
}

class ChildClass extends ParentClass {
public static function who() {
echo __CLASS__;
}
}

ChildClass::test();
?>

?️ Live Output / Explanation

Even though test() is defined in ParentClass, the output is ChildClass because static:: binds to the calling class at runtime.

? Interactive Concept Visualization

Think of Late Static Binding as a dynamic pointer that always refers to the class that initiated the call, not where the method lives.

? Use Cases

  • Static factory methods
  • Reusable framework base classes
  • Static polymorphism
  • Configuration inheritance systems

? Tips & Best Practices

  • Prefer static:: in extensible base classes
  • Avoid mixing static and instance logic unnecessarily
  • Document expected overrides clearly
  • Use abstract static methods for enforceable contracts

? Try It Yourself

  • Create multiple child classes overriding the same static method
  • Replace static:: with self:: and observe the output
  • Build a static factory using Late Static Binding
  • Combine LSB with abstract classes