HomePHP Factoring (X)HTML Objects with Late Static Bindings in PHP 5.3
Factoring (X)HTML Objects with Late Static Bindings in PHP 5.3
In this fifth part of a six-part series on late static bindings, I define some sample classes which are responsible for creating some block-level (X)HTML objects, such as simple divs and paragraphs. Once we get these classes up and running, we'll be ready to test the factories created in the previous part and see if they’re actually as functional as they look.
As you’ll possibly know, PHP 5.3 includes, among a few other useful things, a new feature called Late Static Bindings (LSB). It comes in handy for resolving at runtime from which class a given static method has been called. Even though this major release of the language also offers a solid support for native namespaces and closures, the sole inclusion of LSB is worth the upgrade. This characteristic allows you to create flexible hierarchies of classes that will be used mostly in a static context.
Logically, if you’ve been a loyal follower of this article series, you're already familiar with the benefits provided by LSB. In previous tutorials I demonstrated how to use LSB to build a hierarchy of Singleton registry classes without having to override their static “getInstance()” method, which was impossible to achieve prior to PHP 5.3.
I left off the last installment of the series explaining how to take advantage of the functionality offered by LSB in the object scope by defining a couple of basic factory classes. The first of these classes was an abstract factory, while the second one was a refined implementation of its abstract parent, tasked with creating some block-level (X)HTML objects. However, what made these classes truly interesting (especially the concrete one) was the ability to spawn the above mentioned (X)HTML objects either statically via a static method called “create(),” or dynamically via the pertinent constructor. And guess what? Yes, this functionality was implemented thanks to the use of LSB!
It’s valid to notice, though, that at this stage I only showed the definitions of the factories, but not of the ones that originate the block-level (X)HTML objects. To address this issue, in this penultimate part of the series I’m going to start creating those classes, so you can see how they will be consumed later on by the factories in question.
Let's get started. There are lots of code samples waiting for us!