PHP
  Home arrow PHP arrow Page 2 - 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 - Object-based form generation (continued): explaining the remaining class methods
    ( Page 2 of 4 )

    As I said before, we need to take a detailed look at the rest of the class methods, so if you’re accustomed to coding web forms on a regular basis, they should be fairly easy to understand.

    The “addFormPart()” method is tasked with including any required additional markup that will be rendered outside the iteration for displaying form elements. Say that some tags need to be added to the structure of the form for achieving a better layout. In this case, the method will append the required markup to the overall form’s code, as shown below:

    // add form part
    public function addFormPart($html='<br />'){
        $this->output.=$html;
    }

    As you can see, I’ve specified a default “<br />” tag to be appended when the method is invoked, but this might be easily modified to accept any other required input tag (or a set of tags).

    The other two methods related to including additional markup within the iteration process for displaying form elements are “addElementHeader()” and “addElementFooter()”. Despite the fact they were mentioned previously, here are their respective definitions:

    // add element header
    public function addElementHeader($header){
        $this->elementHeader=$header;
    }
    // add element footer
    public function addElementFooter($footer){
        $this->elementFooter=$footer;
    }

    Similar to “addFormPart()”, the above methods are useful for inserting interspersed markup before and after the code of a form element that has been appended to the form’s output. As expected, these methods implement a simple yet effective way to wrap form fields into regular containers such as <div>, <p> or <table> elements.

    Having explained the core methods of the class, as well as those aimed at adding presentational functionality, the remaining methods are the respective modifiers for the “$name”, “$action” and “$method” properties. They look like this:

    // set form name
    public function setName($name){
        $this->name=$name;
    }
    // set form action
    public function setAction($action){
        $this->action=$action;
    }
    // set form method
    public function setMethod($method){
        if($method!='post'&&$method!='get'){
            throw new Exception('Invalid form method');
        }
        $this->method=$method;
    }

    Because of their extreme simplicity, the above listed methods shouldn’t be difficult to understand, since they’re tasked with setting the values for each form property I mentioned before. Probably, the “setAction()” method might be boosted by checking whether the “$action” parameter is a valid file, but additional features will be left as possible class improvements.

    Finally, the last method to be listed is “getFormCode()”, which simply returns the complete form’s output, to be displayed accordingly:

    // get dynamic form output
    public function getFormCode(){
        return $this->output;
    }

    With all of the methods covered in detail, the next thing to do is set up a practical example, in order to test the capabilities of the recently developed “formGenerator” class and see the real power of design patterns.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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