Eric Hogue on April 14th, 2011

Late static binding is one of the new features of PHP 5.3. It came out almost 2 years ago, but it to me that many programmers around me have no idea about it. Myself, I have learned about it around 6 months ago.

The PHP documentation defines late static binding as a way to “reference the called class in a context of static inheritance.” This definition didn’t really help me the first time I read it. Fortunately, there are more explanations in the documentation, and there are good examples. If you haven’t, you should read it.

How Does It Work

To use late static binding, you need a class that inherits from another one and some static methods that are overridden. Take the following code:

class ParentClass {
    public static function normalCall() {

    public static function lateStaticCall() {

    public static function calledMethod() {
        echo "Called in Parent\n";

class ChildClass extends ParentClass {
    public static function calledMethod() {
        echo "Called in Child\n";

The method normallCall() represent the traditional way of using static functions. The self keyword will call the function in the current class. So no matter if I call it with ParentClass::normalCall() or ChildClass::normalCall(), the calledMethod() of the ParentClass will be called and “Called in Parent” will be printed.

However, the lateStaticCall() method uses the new “static” keyword. With this keyword, the call will be forwarded to the class on witch the original method was called. So ParentClass::lateStaticCall() will end up calling ParentClass::calledMethod() and print “Called in Parent”. But ChildClass::lateStaticCall() will call calledMethod() in the ChildClass and print “Called in Child”.

When Should I Use It

I am really not sure. Personally, I try to avoid static methods. They are often better ways to do it. However, if you have a class hierarchy with static methods and you need to override how they act for some child classes, it can be a solution.

If you used in production code, or know a good reason to use it, I would really appreciate if you leave a comment with it.

Tags: ,

  • Marcel

    You could create a class called Singleton with one static method instance() that returns one singleton instance. Use the function get_called_class the get the name of the subclass.
    class Database extends Singleton {}
    would make the class Database a singleton. (leaving the “singletons are bad” out of discussion.)

  • Hi Marcel,

    Thanks for the example. I see that late static binding could do the job for this. I would probably not use it, singleton are easy to create anyway. But, that looks like a way to use late static binding.

  • James Dunmore

    (Apart from the obvious of avoiding static methods/variables).

    IIRC you’d have issues if you had class constants that are extended (once again, this perhaps isn’t the best way of doing something…. but IF you had a parent database access class with username and password as class constants, without static, you can’t overwrite in a child class – again, an example, not how you would/should write code);

    if you substitue the static:: to self:: you’ll get hello1 not hello2 (note the static function is not needed, the same would apply to an instantiated object calling the const – I think).

    • Hey James,

      I just tried it with constants on instantiated objects and it works just like you said.


  • I never coded with LSB in real applications, despite being on PHP 5.3. I could see a use case for inheritance of static Factory Methods, the only acceptable usage of static methods imho.

    • Hi Giorgio,

      Thanks for the comment. I agree, LSB could be used in this context.

  • Thanks Eric,

    This LSB explanation is a nice complement to the documentation :)


  • namjith

    Good explanation.