PHP Late Static Binding
When I’ve come across Tutorials / Q&A / Forums / Blog posts about PHP’s Late Static Binding feature, I’ve always felt like there’s alot of dancing around the topic and providing highly abstract examples to show what it does and how a person can use it. If you ever need to know how to echo the name of a class using Late Static Binding in PHP, you should have no problem finding a solution out there on the interwebs ☺ Not to say those examples aren’t useful. They are. I just seem to learn new concepts alot quicker when I have real world scenarios / context.
My goal here is to provide an example where one can hopefully see a fabricated, but nonetheless realistic use case for Late Static Binding working with something we’ve all probably encountered at one time or another: comma-separated and tab-separated data. For the sake of brevity and conciseness I’ve left a lot of important code out of this implementation, so I definitely wouldn’t recommend using this code for anything important. We’ll just be looking at a skeleton of an implementation that does what we need it to do in order to run the examples successfully.
Please Note: This is not meant to be an exhaustive list of ways one might use Late Static Binding.
Here’s the code:
The above code contains 1 abstract class GenericParser, and 2 sub classes of GenericParser: CSVParser, and TSVParser. At the end of the code you’ll see the 2 method calls from TSVParser and CSVParser followed by a comment containing what the code outputs.
The GenericParser class contains 2 methods: ‘getTokens’ and ‘convertTextToArray’. The abstract method ‘getTokens’ is implemented by each of the sub classes CSVParser and TSVParser. When the static method ‘getTokens’ is called, each of the 2 implementations will return an array of tokens that are expected to help the ‘convertTextToArray’ method parse the CSV or TSV input.
When you look at each of the CSVParser and TSVParser classes, the arrays returned by the ‘getTokens’ methods give what appear to be reasonable values. For example, *.tsv data is separated by the TAB character, whereas *.csv data is separated by the COMMA character. And for added emphasis I’ve enclosed data of one of the examples in single-quotes and the other in double-quotes.
All the magic happens in line number 9:
$tokens = static::getTokens();
Here the class that the ‘static’ keyword represents is either CSVParser or TSVParser, depending on which one is calling into the method. This ‘static’ keyword gives us a way to “call back” into the calling class statically in a clean way to retrieve the information we need in order to parse the data.
There are several ways you can call into a static method:
There are already alot of ways I can call a static method. Why add more?
//using the class name that implements the static method directly
//static method in same class you're calling from
//static method in parent class to the class you're calling from
//static method of calling class [Late Static Binding]
Assuming ‘ClassName’ in the above example is the name of a class that implements a method called ‘staticMethod’, you can call a static method by naming the class directly. This is the least flexible way to call static methods as compared to the alternatives in that when you hard-code ‘ClassName’ into your class, module, application, you are hard-coding a dependency on the ‘ClassName’ class into the code that is calling it. Alot of times this is going to be fine, but when you’re looking for the greatest amount of extensibility, where you want a way to abstract your code away from any specific class implementation, it’s a good idea to try not to call into the static method by the class name.
The ‘self’ keyword allows you to generically call a static method from within the scope of the class where the static method is defined.
The ‘parent’ keyword allows you to generically call a static method from within the scope of the child class into the parent class (think inheritance, or the ‘extends’ keyword).
The ‘static’ keyword has multiple meanings depending on the context. In the context of calling a static method using Late Static Binding, the ‘static’ keyword in our example code refers to the class that is calling into the ‘convertTextToArray’ method. This allows us to call back (statically) into the calling class.
I hope this has provided some insight into how one might implement Late Static Binding in a realistic scenario using PHP.
I’m relatively new to this “blogging” thing, so if you find any errors please feel free to reach out. I will do my best to make any corrections in a timely manner. Leave me a Twitter handle or a way to recognize you in the ‘Thank You’ section below if I end up using your suggestions.