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

Review: source code of all sample classes developed so far - 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

As I said, LSB makes it really easy to build factory classes that are more flexible than the ones created using a standard approach. But before I elaborate on this concept, I'd like to spend a few moments listing the source code corresponding to all the sample classes defined so far. This way you'll be able to see more clearly how they're going to interact with each other.

Having said that, here's the first of these classes, which is an abstract factory that shows how to use the functionality of LSB in the object scope:

(HtmlElementFactory.php)

<?php

abstract class HtmlElementFactory

{

private $_element;

 

// constructor (Late Static Bindings are used at instance level)

public function __construct($element = '', array $options = array())

{

if ($element !== '')

{

$this->_element = static::create($element, $options);

}

}

 

public function getElement()

{

return $this->_element;

}

 

// implemented by concrete factory classes

public static function create($element, array $options = array())

{

throw new HtmlElementFactoryException('This is an abstract factory and it does not create any concrete HTML element.');

}

}

(HtmlElementFactoryException.php)

<?php

class HtmlElementFactoryException extends Exception {}

Considering that the above class is merely an abstract factory that doesn't create any concrete HTML objects, it's necessary to derive a concrete factory that implements its "create()" static method in a more useful way. The following class performs this task:

(BlockLevelElementFactory.php)

<?php

class BlockLevelElementFactory extends HtmlElementFactory

{

// create a block-level HTML element

public static function create($element, array $options = array())

{

if ($element === 'Div' or $element === 'Paragraph')

{

return new $element($options);

}

}

}

While the definition of this brand new factory class is pretty short, it packages the functionality required for spawning two types of block-level (X)HTML objects, namely divs and paragraphs. But I'm getting ahead of myself; the next step is to show the classes responsible for originating the objects. Therefore, here's yet another abstract class that defines the structure and data of generic (X)HTML elements:

(HtmlElement.php)

<?php

abstract class HtmlElement

{

protected $_class = '';

protected $_id = '';

protected $_content = '';

 

// constructor

public function __construct(array $options = array())

{

if (isset($options['class']))

{

$this->setClass($options['class']);

}

if (isset($options['id']))

{

$this->setId($options['id']);

}

if (isset($options['content']))

{

$this->setContent($options['content']);

}

}

 

// set the 'class' attribute for the HTML element

public function setClass($class)

{

if (is_string($class))

{

$this->_class = $class;

}

return $this;

}

 

// get the assigned 'class' attribute

public function getClass()

{

return $this->_class;

}

 

// set the 'id' attribute for the HTML element

public function setId($id)

{

if (is_string($id))

{

$this->_id = $id;

}

return $this;

}

 

// get the assigned 'id' attribute

public function getId()

{

return $this->_id;

}

 

// set the content for the HTML element

public function setContent($content)

{

if (is_string($content))

{

$this->_content = $content;

}

return $this;

}

 

// get the content of the HTML element

public function getContent()

{

return $this->_content;

}

// implemented by concrete subclasses

abstract public function render();

}

As you can see above, this class implements a bunch of setters and getters to assign and retrieve the values assigned to the "class" and "id" attributes of a given HTML element. Besides, the class permits you to manipulate the element's content via a pair of additional methods, a process that offers nothing especially interesting.

So far, so good. Having defined an abstract class that models the skeleton and basic functionality of generic HTML elements, the last thing we need to do is  create a couple of additional subclasses, tasked with rendering divs and paragraphs. Here they are:

(Div.php)

<?php

class Div extends HtmlElement

{

public function render()

{

return '<div class="' . $this->getClass() . '" id="' . $this->getId() . '">'. $this->getContent() . '</div>';

}

}

(Paragraph.php)

<?php

class Paragraph extends HtmlElement

{

public function render()

{

return '<p class="' . $this->getClass() . '" id="' . $this->getId() . '">'. $this->getContent() . '</p>';

}

}

Mission accomplished. Now that you've seen all of the sample classes developed so far, it's time to put them to work together and do something really useful. To be frank, the first thought that comes to my mind is to create some objects directly using the earlier "Div" and "Paragraph" classes and render a few of these HTML elements on screen.

But wait a minute! This was already demonstrated in the previous part of the series, so what more can be done with these classes? Well, as I said in the introduction, my purpose here is to prove that the concrete "BlockLevelElementFactory" factory (remember that one, right?) is capable of creating those objects either by using its constructor or via its static "create()" method.

That's precisely the topic that I plan to discuss with you in the next section. Therefore, to learn more on it, click on the link below and keep reading.



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