Home arrow PHP arrow 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.

  1. A Final Example of Late Static Bindings in PHP 5.3
  2. Review: source code of all sample classes developed so far
  3. Putting the BlockLevelElementFactory class into action
  4. Creating (X)HTML objects in a static context
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
May 26, 2010

print this article



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

echo $div->render();

// 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

echo $par->render();

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.

Final thoughts

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.

See you in the next PHP tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: