Static Interfaces and Initialization in PHP

Forums Personal Topics What I’m Up To Static Interfaces and Initialization in PHP

This topic contains 1 reply, has 1 voice, and was last updated by  josh October 30, 2017 at 5:40 am.

  • Author
    Posts
  • #5935

    josh

    The constructs self::some_static_member_or_function and static::some_static_member_or_function are similar in PHP and can often be used interchangeably. I come across more code using “static” and it is thematic because both related to variables and functions that are declared “static” (only choice). However, they are different, static:: is looser, and, IMO, kind of wild and dangerous.

    In a situation where classes B and C are both derived from A, but neither B nor C is derived from the other, static:: allows late binding of calls within B to static objects and data members from C, just in case a chain of function calls descended through C to get to code in B. This in counter-intuitive from an OOP POV. There is no real way to prevent it happening by accident either. So it is better to use self:: by default and only use static:: where it is actually required for late binding, taking care to check for potential name collisions in even unrelated classes implementing static variables or methods with the same names.

You must be logged in to reply to this topic.