Home arrow PHP arrow Page 3 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class

A smarter factory: applying Polymorphism within the “formElementFactory” class - PHP

In this third part of the series, I will explain how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects. This will boost the functionality of the factory class by simplifying the process for rendering form elements.

  1. The Singleton and Factory Patterns in PHP: a rendering-capable factory class
  2. The Factory pattern in a real application: a quick look at the “formElementFactory” class
  3. A smarter factory: applying Polymorphism within the “formElementFactory” class
  4. Polishing the Page
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
November 16, 2005

print this article



Essentially, making the form element factory a little “smarter” implies boosting it with the ability to exploit the polymorphic nature of form objects. Since each one of these elements presents the same “getHTML()” method, it’s extremely easy to encapsulate it within the class structure and return the specific (X)HTML code.

Considering this, here is the improved version of the form element factory class:

abstract class formElementFactory{
    private function __construct(){}
    public static function createElement($type,$attributes=array()){
            throw new Exception('Invalid method parameters');
        // instantiate a new form element object
        $formElement=new $type($attributes);
        // return form object’s HTML
        return $formElement->getHTML();

Quite simple, right? Now, the class is capable of directly returning the (X)HTML code that corresponds to each form object. The overall form generation process is now a little more efficient, reduced to something like this:

// make array with parameters to be passed to the class
foreach($formElements as $element=>$attributes){
    // display form elements
    echo formElementFactory::createElement($element,$attributes).'<br />';

Now the code looks stronger and more compact. What I’ve done is simply built a recursive array with the parameters to be passed to the class, in order to display a couple of input fields (a text box and a password field), and a submit button. Finally, the form is roughly displayed, by calling the static “createElement()” method within a “foreach” loop.

To have a better idea of the functionality of the boosted factory class, here is the crude code for displaying several form elements:

// display an input text box
echo formElementFactory::createElement('textinput',array

// display a password field
echo formElementFactory::createElement('passwordinput',array

// display a hidden field
echo formElementFactory::createElement('hiddeninput',array

// display a submit image
echo formElementFactory::createElement('imageinput',array

// display a file upload field
echo formElementFactory::createElement('fileinput',array

// display a radio button
echo formElementFactory::createElement('radiobutton',array

// display a text area
echo formElementFactory::createElement('textarea',array
('name'=>'name','value'=>'fill this field'));

// display a check box
echo formElementFactory::createElement('checkbox',array

// display a button
echo formElementFactory::createElement('button',array

// display a submit button
echo formElementFactory::createElement('submitbutton',array

// display a reset button
echo formElementFactory::createElement('resetbutton',array

// display a select box
echo formElementFactory::createElement('selectbox',array

Of course, this is an extremely ineffective implementation of the factory class, but it is useful to have a set of illustrative examples covering how each form element is rendered.

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