What Exactly Are Late Static Bindings in PHP

What exactly are late static bindings in PHP?

You definitely need to read Late Static Bindings in the PHP manual. However, I'll try to give you a quick summary.

Basically, it boils down to the fact that the self keyword does not follow the same rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect.

Late static binding introduces a new use for the static keyword, which addresses this particular shortcoming. When you use static, it represents the class where you first use it, ie. it 'binds' to the runtime class.

Those are the two basic concepts behind it. The way self, parent and static operate when static is in play can be subtle, so rather than go in to more detail, I'd strongly recommend that you study the manual page examples. Once you understand the basics of each keyword, the examples are quite necessary to see what kind of results you're going to get.

PHP Late Static Binding Use

In this particular case using $c::MY_CONST, self::MY_CONST, static::MY_CONST, myClass:MY_CONST will have the same effect. But look your class is not declared as final and can be extended and when it's extended static keyword will point to the const using late binding.

Using a late binding always come with the cost, but come on it's not C++, you don't program a driver on low level with low resources. The cost won't be noticeable at all. However, not using static may lead to some hard to detect bugs in the future.

In PHP manual you can also find interesting thing about late binding in PHP

Late static bindings work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non static method calls, it is the class of the object.

This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information.

Why doesn't late static binding work with variables in PHP 5.3?

Late static binding will only work for new definitions of variables / methods. Thus, in your example, the $color property of Super will always be modified instead of ChildA or ChildB. To make use of late static binding, you need to use the static keyword instead of self. Furthermore, you need to redefine the $color property of your ChildA and ChildB classes:

class Super {

protected static $color;

public static function setColor($color){
// static instead of self
static::$color = $color;
}

public static function getColor() {
// static instead of self
return static::$color;
}

}

class ChildA extends Super {
protected static $color;
}
class ChildB extends Super {
protected static $color;
}

ChildA::setColor('red');
ChildB::setColor('green');

echo Super::getColor(); // prints nothing (NULL = ''), expected
echo ChildA::getColor();// prints red
echo ChildB::getColor();// prints green

Is it possible to overuse late static binding in PHP?

So, to wrap it all up, are these uses (and similar ones) of late static binding are an overkill? Is there any noticeable performance hit? Also, does frequent use of late binding reduce the overall performance boost given by opcode caches?

The introduction of late static binding fixes a flaw in PHP's object model. It's not about performance, it's about semantics.

For example, I like to use static methods whenever the implementation of the method doesn't use $this. Just because a method is static doesn't mean to say that you don't want to override it sometimes. Prior to PHP 5.3, the behavior was that no error was flagged if you overrode a static method, but PHP would just go ahead and silently use the parent's version. For example, the code below prints 'A' before PHP 5.3. That's highly unexpected behavior.

Late static binding fixes it, and now the same code prints 'B'.

<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who();
}
}

class B extends A {
public static function who() {
echo __CLASS__;
}
}

B::test();
?>

Method Overriding vs Late Static Binding?

Late static binding is essentially method overriding for static methods. There are some subtle differences in how they are actually carried out by the compiler. See What exactly are late static bindings in PHP?



Related Topics



Leave a reply



Submit