PHP
  Home arrow PHP arrow Page 3 - Controlling Online Forms with Builder Objects in PHP 5
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

Controlling Online Forms with Builder Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-10-11


    Table of Contents:
  • Controlling Online Forms with Builder Objects in PHP 5
  • Defining a form element class
  • Creating a form builder class
  • Constructing a basic online form

  • 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


    Controlling Online Forms with Builder Objects in PHP 5 - Creating a form builder class
    ( Page 3 of 4 )

    In the section that you just read, you saw how the "FormElement" class was defined to have at its disposal a target object to work with. Now, it's time to move forward and see how a second class, that is the one called "builder," can be created in such a way that it'll be able to construct different form components.

    By using a similar approach to the one utilized for defining the "FormElement" class that was shown before, I'll first create an abstract form builder class, and then derive a single child from it, which will expose some concrete methods for building online forms.

    In accordance with the concepts expressed above, here is the definition of the first abstract class, which I called obviously "AbstractFormBuilder." Please, have a quick look at its simple signature:

    // define abstract 'AbstractFormBuilder' class abstract class AbstractFormBuilder{ abstract function getForm(); }

    As you can see, there's not much to be said about the above class. The only thing worth highlighting is the declaration of the "getForm()" method, which when concretely implemented will return the corresponding (X)HTML markup of the online form being constructed.

    After showing the signature of the base "AbstractFormBuilder" class, pay attention to the next one, which offers a concrete implementation for the method defined previously:

    // define concrete 'FormBuilder' class class FormBuilder extends AbstractFormBuilder{ private $html=array(); private $action; private $method; public function __construct($action='',$method='post'){ // setup form attributes        $this->action=!$action?$_SERVER['PHP_SELF']:$action;      $this->method=$method!='post'||$method!
    ='get'?'post':$method; } // add form element public function addElement($type='text',$attributes=array
    ('name'=>'default'),$options=array()){

    if(!$elem=new FormElement($type,$attributes,$options)){ throw new Exception
    ('Failed to instantiate '.$type.' object'); }      $this->html[]=$elem->getHTML(); } // add form part public function addFormPart($formPart='<br />'){       $this->html[]=trim($formPart)==''?'<br />':$formPart; } // get web form public function getForm(){        $formOutput='<form action="'.
    $this->action.'" method="'.$this->method.'">';       foreach($this->html as $html){             $formOutput.=$html; }        $formOutput.='</form>'; return $formOutput; } }

     

    If you take some time and analyze the source code of the above class, then you'll understand in a snap what it does. Basically, this sub class is capable of building a typical online form, based on a few input parameters, such the $method and $action variables, as well as the set of arguments passed to its "addElement()" method.

    As you'll possibly realize, this method acts like a true form element factory (notice the instantiation of "FormElement" objects from inside the method's scope), and uses the advantages of polymorphism, in order to generate the (X)HTML markup that corresponds to each form element being created.

    Lastly, the functionality of the "getForm()" method is limited simply to returning to calling code the form's complete output. Due to its extreme simplicity, I won't stop long on this one, therefore let me recapitulate for a moment, and show you what I have accomplished up until now.

    At this point I created the two first components required for getting the builder pattern working, that is the target object to be created (in this case represented by the "FormElement" class), and the respective form builder. Indeed, it looks like I'm very close to implementing correctly the pattern in question.

    However, before doing that, I'd like to show you a short and comprehensive example, aimed at demonstrating how the set of classes that I wrote earlier are really functional when building web forms.

    To see how this will be done, please go ahead and read the following section.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT