HomePHP Registry Classes in Action with Late Static Bindings in PHP 5.3
Registry Classes in Action with Late Static Bindings in PHP 5.3
In this second part of the series, I show you how useful late static bindings can be for dealing with a hierarchy of classes in static environments. In this case, the classes that compose that hierarchy are simple implementations of the registry design pattern, but this feature can be utilized in different situations and yield similar results.
The release of PHP 5.3.0 a few months ago put a big smile on the face of many developers. This major upgrade of the language has been packaged with some new and exciting features that allow you to build more robust and reliable applications, particularly when using the object-oriented paradigm. By far, the most relevant improvement in the language has been the support for native namespaces, which elegantly and maturely prevents clashes that might occur when naming classes. Itís fair to note, however, that the incorporation of anonymous functions, along with late static bindings, are useful enhancements that deserve special mention as well.
Even though theyíve been underrated from the very beginning by some programmers, the truth is that late static bindings (LSB) are a powerful feature that permits you to solve common problems that arise when working when hierarchies of classes in a static context, although itís also possible to use LSB at instance-level with the same ease.
Of course, explaining the theoretical facets of LSB isnít very useful. The best way to understand its actual functionality is by seeing an example. With that premise in mind, in the introductory part of this series I demonstrated how to use LSB to create a simple hierarchy of classes composed of a couple of registries. The first of these classes was an abstract registry, while the second was a refined implementation of its abstract parent, responsible for storing and fetching data from a private array.
Since the array-based registry inherited a Singleton static method from its abstract parent, LSB was used to return an instance of the calling class at runtime, which was impossible to achieve prior to PHP 5.3. While this sounds all well and good, I have to admit that at this point I've shown only the definitions of these sample classes, and not how to use them in a concrete case, which isnít very helpful from a didactic point of view.
Thus, in the lines to come Iím going to set up an example that will illustrate how work with this pair of registry classes, so you can see how powerful LSB actually is. So click on the link below and begin reading.