PHP
  Home arrow PHP arrow Page 3 - The Singleton and Factory Patterns in ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · The final part of this series goes trough the makings of a form generator class, by...
       · Just wondering how you would have a web-designer and a developer team-develop the...
       · Well, thank you for the comments on my article, since that proves it was quite...
       · Widgets would be my suggestion as well. Just ensuring that those less versed in...
       · Thanks to you for the quick and positive response! Also, I'm glad to know that you...
       · Excellent write up Alejandro. May I ask for the reason this last part is written for...
       · Hello Matthijs,Thank you for the compliments on this PHP article series. It's...
       · Hi Alejandro,Thanks for your reply. Your suggestion did solve the problem I had!...
       · Hi Matthijs,Good to know that your problem is now fixed up. Of course it's...
       · That's really helpful Alejandro. I'll take your suggestions and build something. I...
       · Thank you Matthijs. I'm happy to know that my suggestions were helpful to you.My...
       · Hi Alejandro,Thinking about patterns, and comparing your earlier articles about...
       · Hello Matthijs,Well, it's not a hard question, but perphaps it's a little long...
       · Your comments are certainly helpful. And thanks for the links. I did see the site...
       · Hi, Mathjis. It's good to know that my answers were useful to you, as well as the...
       · Thanks for a great article it's gone along way in selling the benefits of OOP in...
       · Thank you for the kind comments on my article. I really appreciate your feedback....
       · Many thanks for the quick reply Alejandro. I'll give that a go - it should give some...
       · Thanks - worked a treat. I might try using this pattern for field validation as...
       · Thank you. Also, I'm glad to know that this form generator class has been useful to...
       · Thank you for the comments. Yes, adding form validation routines would be a nice...
       · Your form generator example must be excellent, as it was very straightforward for me...
       · Hello - Your form script is great - I'm just wondering how you would include a users...
       · Thank you I was having the same issue with this code.
       · Hi Regan,Thanks for the kind words on my PHP article. There’s a few simple ways...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT