PHP
  Home arrow PHP arrow Page 3 - The Singleton and Factory Patterns in PHP: Building a Form Generator 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? 
PHP

The Singleton and Factory Patterns in PHP: Building a Form Generator Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2005-12-07


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: Building a Form Generator Class
  • Object-based form generation (continued): explaining the remaining class methods
  • A practical example: putting the “formGenerator” class to the test
  • The source code box: listing the full developed classes

  • 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: Building a Form Generator Class - A practical example: putting the “formGenerator” class to the test
    ( Page 3 of 4 )

    Despite the rather complicated-sounding nature of the phrase, building object-based forms is a really easy thing to do. With all of the form element classes already defined, as well as the “formElementFactory” class acting as a real factory that returns form objects, setting up the “formGenerator” class for displaying layout-controlled web forms is a pretty straightforward task.

    To test the functionality of the “formGenerator” class, I’ll show an example that builds a simple login form, comprised of “username” and “password” fields, together with the corresponding login button. Also, I’ll add some additional markup within the form, to demonstrate the control over the general layout that the class offers. So, the sample code looks like this:

    // define parameters for the formGenerator class
    $formElements=array('textinput'=>array
    ('name'=>'username','maxlength'=>'20'),'passwordinput'=>array
    ('name'=>'password','maxlength'=>'20'),'submitbutton'=>array
    ('name'=>'login','value'=>'Log in'));
    // instantiate a new formGenerator object
    $fg=new formGenerator($formElements);
    // add element labels
    $fg->addFormPart('<table style="float:left;"><tr><td>User
    Name<td></tr><tr><td>Password<td></tr></table>');
    // add a table to the form code
    $fg->addFormPart('<table>');
    // add a table row as element header
    $fg->addElementHeader('<tr><td>');
    // add closing tags
    $fg->addElementFooter('</td></tr>');
    // generate form
    $fg->createForm();
    // add a closing </table> tag
    $fg->addFormPart('</table>');
    // display the form
    echo $fg->getFormCode();

    As you can see, although the above snippet takes up only a few lines of code, it is actually very powerful. First, the script builds a recursive array with the name and parameters of each form object to be created. Here is the line responsible for doing this:

    // define parameters for the formGenerator class
    $formElements=array('textinput'=>array
    ('name'=>'username','maxlength'=>'20'),'passwordinput'=>array
    ('name'=>'password','maxlength'=>'20'),'submitbutton'=>array
    ('name'=>'login','value'=>'Log in'));

    Then, a new “formGenerator” object is instantiated and the above array is passed to the constructor. Let’s stop briefly at this point and see what’s happening here: if we return to the definition of the class, it’s clear that only two lines are required to generate the main source code of the form, because all of the internal processing for factoring form objects is handled inside the class context. This simple concept demonstrates the real power of the factory pattern, when correctly applied.

    The rest of the code uses the “addFormPart()” method to roughly build a table containing the labels for each field, along with the “addElementHeader()” and “addElementFooter()” methods to wrap up form fields into a containing <table> structure.

    Finally, the complete (X)HTML markup is displayed by calling the “getFormCode()” method, like this:

    echo $fg->getFormCode();

    By this point, you’ve been provided with the required background to use some design patterns in order to build web forms based on an object-oriented method. Definitely, repetitive and boring tasks involved in the development of web programs such as form generation, can be easily turned into flexible processes by conceiving them as an issue feasible to be solved through the object-oriented paradigm.

    To wrap up, for those developers who want to have available “in one place” all the source code developed for PHP5, I’ll offer a list of each class used to build object-based forms.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT