Home arrow PHP arrow Page 4 - Late Static Bindings in PHP 5.3 with the Abstract Factory Pattern

Subclassing the previous abstract factory class - PHP

In this fourth part of the series, I demonstrate how Late Static Bindings can be useful for implementing the abstract factory design pattern. The feature will be used at the instance level within the constructor of an abstract factory class, where at least one of its concrete implementations is tasked with factoring a couple of block-level (X)HTML objects, such as divs and paragraphs.

TABLE OF CONTENTS:
  1. Late Static Bindings in PHP 5.3 with the Abstract Factory Pattern
  2. Review: using Late Static Bindings to build registry classes
  3. Creating an abstract factory class
  4. Subclassing the previous abstract factory class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
May 24, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Considering that the constructor of the earlier abstract factory class will invoke the static “create()” method at runtime due to the internal use of Late Static Bindings, building a concrete factory that returns to client code a couple of block-level (X)HTML objects is as simple as defining the following class: 

(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);

        }

    } 

}

Mission accomplished. At this point, I created a simple, yet functional, concrete factory class which overrides the static “create()” method defined by its abstract parent. In this case, the method is responsible for factoring (X)HTML objects of type “Div” and “Paragraph” respectively, but naturally it’s possible to extend its functionality by adding support for other block-level objects. Nonetheless, the most interesting facet of this class is its ability to factor the objects either statically or through its constructor. This shows how useful Late Static Bindings actually are, even when used in the object scope.

And with this final code sample I’m finishing this chapter of the series. As usual, feel free to tweak the source code of all the classes shown in this tutorial. Doing so will help you to better understand how to incorporate the functionality provided by LSB into your own object-oriented PHP applications.

Final thoughts

That’s it for the moment. Over this fourth part of the series, I demonstrated how Late Static Bindings can be quite useful when it comes to implementing the abstract factory design pattern. In the above code samples, this feature was used at the instance level within the constructor of an abstract factory class, where at least one of its concrete implementations was tasked with factoring a couple of block-level (X)HTML objects, such as divs and paragraphs.

Of course, the next logical step we must take is to define the classes responsible for creating the aforementioned (X)HTML objects. That’s exactly the topic that I plan to cover in the forthcoming tutorial of the series. Now that you know what to expect from the next installment, you don’t have any excuses to miss it!



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