HomePHP Page 4 - A Final Example of Late Static Bindings in PHP 5.3
Creating (X)HTML objects in a static context - PHP
Late Static Bindings (LSB), a powerful feature bundled with PHP 5.3, are much easier to master than you might think. Achieving intimate knowledge of them requires only an average background in working with objects and classes. This article concludes a six-part series that shows you what you can do with LSB.
As I said in the preceding segment, I'd like to end this tutorial by demonstrating how to use the earlier factory class for spawning a div and a paragraph object via the factory's create()" method. The script below shows pretty clearly how to achieve this. Check it out:
// create a div object using the static 'create()' method
$div = BlockLevelElementFactory::create('Div', array('class' => 'divclass', 'id' => 'divid', 'content' => 'This is the content of the div.'));
// render the div element on screen
// create a paragraph object using the static 'create()' method
$par = BlockLevelElementFactory::create('Paragraph', array('class' => 'parclass', 'id' => 'parid', 'content' => 'This is the content of the paragraph.'));
// render the paragraph element on screen
See how convenient LSB really are when it comes to building a flexible factory class that can be used either statically or at instance level? I guess you do! In the above example, the factory has been utilized in a more traditional way. It creates the pertinent (X)HTML objects in a static context. Nonetheless, as you saw a moment ago, nothing stops you from performing the same creation process using its constructor.
And with this example I'm finishing this tutorial and this series. As usual, feel free to edit all of the code samples included in the article. By doing so, you'll surely arm yourself with a more thorough knowledge of using Late Static Bindings in all sorts of creative ways.
It's hard to believe, I know, but we've come to the end of this series. Hopefully, all of the code samples shown in this group of tutorials have provided you with a more solid background in how to use Late Static Bindings in different contexts and use cases.
Ranging from building versatile registry classes, to constructing abstract and concrete factories, LSB are indeed a powerful tool that allows to add flexibility and efficiency to classes in a plethora of environments and situations. So, if you're still hesitating about the real functionality of this feature, leave your doubts behind once and for all and start using it when developing your own PHP applications.