PHP
  Home arrow PHP arrow Page 3 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

The Singleton and Factory Patterns in PHP: a rendering-capable factory class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2005-11-16


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: a rendering-capable factory class
  • The Factory pattern in a real application: a quick look at the “formElementFactory” class
  • A smarter factory: applying Polymorphism within the “formElementFactory” class
  • Polishing the Page

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Singleton and Factory Patterns in PHP: a rendering-capable factory class - A smarter factory: applying Polymorphism within the “formElementFactory” class
    ( Page 3 of 4 )

    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()){
            if(!class_exists($type)||!is_array($attributes)){
                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
    $formElements=array('textinput'=>array
    ('name'=>'username','value'=>'','maxlength'=>'20'),
    'passwordinput'=>array
    ('name'=>'password','value'=>'','maxlength'=>'20'),
    'submitbutton'=>array
    ('name'=>'name','value'=>'Send'));
    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
    ('name'=>'name','value'=>'','maxlength'=>'20'));

    // display a password field
    echo formElementFactory::createElement('passwordinput',array
    ('name'=>'name','value'=>'','maxlength'=>'20'));

    // display a hidden field
    echo formElementFactory::createElement('hiddeninput',array
    ('name'=>'name','value'=>'default'));

    // display a submit image
    echo formElementFactory::createElement('imageinput',array
    ('name'=>'name','src'=>'submit.gif'));

    // display a file upload field
    echo formElementFactory::createElement('fileinput',array
    ('name'=>'name','value'=>'1'));

    // display a radio button
    echo formElementFactory::createElement('radiobutton',array
    ('name'=>'name','value'=>'1','checked'=>'true'));

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

    // display a check box
    echo formElementFactory::createElement('checkbox',array
    ('name'=>'name','value'=>'1','checked'=>'true'));

    // display a button
    echo formElementFactory::createElement('button',array
    ('name'=>'name','value'=>'button'));

    // display a submit button
    echo formElementFactory::createElement('submitbutton',array
    ('name'=>'name','value'=>'Send'));

    // display a reset button
    echo formElementFactory::createElement('resetbutton',array
    ('name'=>'name','value'=>'Reset'));

    // display a select box
    echo formElementFactory::createElement('selectbox',array
    ('name'=>'name','size'=>'1','options'=>array
    ('1'=>'value1','2'=>'value2')));

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek