Drupal 8 Development Mastery (8): Modern PHP Programming: Late Static Binding

We explore the concept of Late Static Binding in PHP: Official definition, the problems it solves, how it solves it using very simple examples, how it is important to Drupal 8, and references to more in-depth treatments.

Drupal 8 Development Mastery (8): Modern PHP Programming: Late Static Binding

I must confess, I have only been vaguely aware of the term 'Late Static Binding' before shifting interest to Drupal 8 development. My hazy understanding then was that it's one of those subtle and difficult-to-grasp features of PHP that is generally not used often.

I will go about explaining Late Static Binding by first defining it technically. A definition, I must warn, you may not understand if you are just hearing the term for the first time (I didn't my first time). But bear with me, I will shed more light on the definition right after. I will first explain the problem it tries to solve, then introduce how it solves it, all using very simple examples. As I keep repeating at every opportunity through out this series, I am assuming some basic knowledge of object-oriented programming in PHP.

The Official Definition

According to the PHP manual Late Static Binding is a feature introduced in PHP version 5.3 “which can be used to reference the called class in a context of static inheritance.” This made little sense to me at first. But I dug further and the following is what I now understands about  LSB.

The clue to what it means is in the name, but first a look at the problems it tries to solve.

The Problem

The problem this feature attempts to solve lies in the limitation of the PHP keyword ‘self’. Basically the self keyword is used to access static properties and methods of classes. It is used as a variable reference to the class in which it is being called (similar to $this in some other languages). An simple example would be appropriate here:

Let us create a class ‘Car’ with a string variable property $name and a method that returns the value of that variable, whatAreYou(). Note the use of the ‘self’ keyword here.

<?php 

class Car
{
    protected static $name = 'I am a Car';

    private static function whatAreYou()
    {
        echo self::$name;
    }
}
?>

 

Now lets create another class that extends this ‘Car’ class:

<?php

class Lexus extends Car
{
    protected static $name = 'I am a Lexus RX330 Car';
}

?>

 

NOTE: A refresher reminder: The static keyword makes it possible for you to reference class variables and methods in your code without first instantiating it as an object. But you already know that.

Now lets run our code and see the result:


Lexus::whatAreYou();

This will output: "I am a Car". Not what we expected.

The reason for this is in how PHP deals with the ‘self’ keyword. It permanently ‘binds’ (static binding) the "self" keyword to the class ‘Car’ early on in the compile process in this case. Because inheritance happens later on, all static references to the function whatAreYou() will return the same value (the first bound value) no matter the overriding value of $name in the inheriting classes.

In contrast to our topic of interest we may call this case ‘Early Static Binding’ (Not official, I just made this up).

The Solution

Now let's employ our new feature to solve this problem. The solution is simply to replace the ‘self’ keyword in the above with the keyword ‘static’ as below:

<?php

class Car
{
    protected static $name = 'I am a Car';

    private static function whatAreYou()
    {
        echo static::$name;
    }
}

class Lexus extends Car
{
    protected static $name = 'I am a Lexus RX330 Car';
}

?>

 

Now lets run the same code as previously and see the result:

 Lexus::whatAreYou();

This will now output: "I am a Lexus RX330 Car". Just what we expected.

 

What happened here is that the static keyword is being bound later on in the compile process when inheritance is being processed (last bound value). The keyword "static" is now being bound to the inheriting Lexus class at the later inheriting stage, hence ‘Late Static Binding’.

Usages In General

As for the usefulness of this feature, I must confess, I am just learning this for the first time. I can’t imagine any practical use case for it right now. But I suspect it might not be very popular, as from my searches online, I couldn’t find any usage example I could understand.

Usages In Drupal 8

Another confession: as I have not dug any deeper into how Drupal 8 works internally as at the time of writing this, the only link I know of between Drupal 8 and LSB is from various Drupal 8 introductory articles touting the wide use of LSB in Drupal 8. These articles go as far as promoting the mastery of LSB as being important towards Drupal 8 development mastery. Given that these articles are from trusted sources from the Drupal community, I have absolutely no reason to question these claims.

If these guys say it important then I will swallow that line, hook and sinker, and invest the time and effort to learn it without asking any further question. QED. And I suggest you do too.

Conclusion

References

Extra Reference

While researching for this topic, I came across a PHP tutorial lessons that I think would be very useful for those trying to quickly learn certain PHP topics. It is geared towards both novices and more knowledgeable PHP programmers. It is a quick read and easily digestible without getting too technical. It introduces both basic topics and more advanced topics like OOP, including some topics that would be valuable to mastering Drupal 8 development.

http://www.zentut.com/php-tutorial/

This concludes the Modern PHP Programming sub-section of the Technology Stacks section. Next I take on one of my most anticipating topic of this series: Symfony!

Categories: 

Add new comment