Monday, 2 December 2024

Late static binding and early binding in PHP

Late static binding and early binding are concepts in PHP that describe how methods or properties are resolved when a class hierarchy is involved. The key difference lies in when the resolution occurs and which class context is used.


Early Binding (self)

  • Definition: Early binding resolves the method or property to the class in which it is defined, regardless of the class from which it is called.
  • Behavior:
    • Uses self to reference the class where the code is written.
    • The binding happens at compile time.
    • Does not respect polymorphism in class hierarchies.

Example:

class ParentClass { public static function who() { return "ParentClass"; } public static function callWho() { return self::who(); // Early binding to ParentClass::who() } } class ChildClass extends ParentClass { public static function who() { return "ChildClass"; } } echo ParentClass::callWho(); // Output: ParentClass echo ChildClass::callWho(); // Output: ParentClass

Explanation:

  • self::who() in callWho() binds to the method who() in ParentClass because self is resolved at compile time.
  • Even when ChildClass calls callWho(), it does not use ChildClass::who().

Late Static Binding (static)

  • Definition: Late static binding resolves the method or property to the class that is actually calling the method (runtime class context).
  • Behavior:
    • Uses static to reference the calling class.
    • The binding happens at runtime.
    • Respects polymorphism in class hierarchies.

Example:

class ParentClass { public static function who() { return "ParentClass"; } public static function callWho() { return static::who(); // Late static binding } } class ChildClass extends ParentClass { public static function who() { return "ChildClass"; } } echo ParentClass::callWho(); // Output: ParentClass echo ChildClass::callWho(); // Output: ChildClass

Explanation:

  • static::who() in callWho() resolves to the who() method in the calling class.
  • When ChildClass calls callWho(), it dynamically binds to ChildClass::who().

Comparison Table

FeatureEarly Binding (self)Late Static Binding (static)
Resolution TimeCompile timeRuntime
Reference ContextThe class where the method is definedThe class that is calling the method
PolymorphismDoes not respect polymorphismRespects polymorphism
Keyword Usedselfstatic
Behavior with InheritanceAlways binds to the parent class method (if called in the parent)Dynamically binds to the calling class method

Key Points to Consider

  • Use early binding (self) when the behavior should always be tied to the parent class and should not change with inheritance.
  • Use late static binding (static) when you want polymorphic behavior, allowing the method or property resolution to adapt to the calling class at runtime.

Practical Example Comparing Both

class ParentClass { public static function who() { return "ParentClass"; } public static function earlyCall() { return self::who(); // Early binding } public static function lateCall() { return static::who(); // Late static binding } } class ChildClass extends ParentClass { public static function who() { return "ChildClass"; } } echo ParentClass::earlyCall(); // Output: ParentClass echo ChildClass::earlyCall(); // Output: ParentClass (early binding) echo ParentClass::lateCall(); // Output: ParentClass echo ChildClass::lateCall(); // Output: ChildClass (late static binding)

This highlights how self locks behavior to the defining class, while static allows dynamic resolution based on the calling class.

Thank you.

No comments:

Post a Comment

Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...