Home arrow PHP arrow Page 3 - A Final Example of Late Static Bindings in PHP 5.3

Putting the BlockLevelElementFactory class into action - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Definitely, one of the most interesting facets of LSB is that, despite what the name suggests, they can be used in the object scope as well. To demonstrate this, below I wrote a simple script which uses the previous concrete factory to create and render a basic div object using the class' constructor. Look at the following code snippet, please:

// create an instance of the BlockLevel Factory and factory a div element

$blFactory = new BlockLevelFactory('Div');

$div = $blFactory->getElement();

echo $div->setClass('myclass')

->setId('myid')

->setContent('This is the content of the div.')

->render();

That was easy to code and read, wasn't it? As you can see, in this case the div object has been returned to client code using the factory's "getElement()" method. However, what makes this example especially exciting is that the object in question was created at the instance level, and not in a static context as one might expect.

What's more, if you're interested in seeing how this approach can be used for spawning a paragraph object instead of a div, here's how it can be done in a few simple steps:

// create an instance of the BlockLevelElementFactory and factory a paragraph element

$blFactory = new BlockLevelElementFactory('Paragraph');

$par = $blFactory->getElement();

echo $par->setClass('myclass')

->setId('myid')

->setContent('This is the content of the paragraph.')

->render();

Since this example looks pretty similar to the one that creates a div object, I'm not going waste your time explaining its underlying logic. Instead, it's time to show how to use the previous factory class for spawning the same (X)HTML objects that you saw before, but this time by using its static "create()" method directly.

The full details of this process will be covered in the last section, so click on the link below and read the lines to come.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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