Late static Bindings (PHP >= 5.3.0)

in programming languages, binding is the association of the values with the identifiers.

  • association before running: early binding
  • association at runtime: late binding (also called virtual or dynamic)
PHP5 supports the classic late binding. At runtime, PHP5 distinguishes the superclass from the subclasses object, respecting the overridden methods rules.
The late binding for static methods are available only from PHP 5.3.0

Late static binding (PHP >= 5.3.0)

Let’s consider binding for static methods:

Consider this class, in which the static method f() calls (using “self::”) the static internal method print() which prints “A” :

class A {
static function print() { print
“A”; }
static function
f() { print self::print(); }
}

Now consider the extension “B” which overrides the method print() which prints “B”

class B extends A {
static function print() { print
“B”; }
// f() inherited !
}

B::f(); // A !! no binding ! “self::” point to superclass, not to subclass !

——————————————

PHP 5.3.0 supports the pointer “static::” to support Late Static binding

class A {
static function print() { print
“A”; }
static function
f() { print static::print(); }
}
class
B extends A {
static function print() { print
“B”; }
// f() inherited !
}

B::f(); //B. late static bindings!!


In according to Late Static Bindings, if we call the static method f() of the class B, the inherited method f() will consider static as a pointer to the current class, that is B, and the the invoked method will call print() of the subclass B (and will print “B”).
In other words, “static” is a pointer to the “current class”