Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Late Static Binding

DZone's Guide to

Late Static Binding

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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() {
self::calledMethod();
}

public static function lateStaticCall() {
static::calledMethod();
}

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Eric Hogue, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}