PHP
  Home arrow PHP arrow Page 4 - Developing a Form Director 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

Developing a Form Director Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2006-10-18


    Table of Contents:
  • Developing a Form Director Class
  • Refreshing some core concepts
  • The form builder class
  • Defining a form director class

  • 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


    Developing a Form Director Class - Defining a form director class
    ( Page 4 of 4 )

    Undoubtedly, when it comes to applying the builder pattern with PHP, the heart of the matter is the director class. After all, this one is the boss that instructs the builder on how to construct a given object.

    In this case, since the builder in question is responsible for building online forms, logically the director must be represented by another class, capable of giving some specific instructions that are aimed at controlling the whole process for creating a web form.

    Based on this simple principle, below I coded two new classes. The first one is the abstract form director, and the second one consists of the corresponding sub class that implements concretely all the required methods. Please, examine them:

    // define abstract 'AbstractFormDirector' class abstract class AbstractFormDirector{ abstract function __construct(AbstractFormBuilder $formBuilder); abstract function buildForm(); abstract function getForm(); } // define concrete 'FormDirector' class class FormDirector extends AbstractFormDirector{ private $formBuilder=NULL; public function __construct(AbstractFormBuilder $formBuilder){ $this->formBuilder=$formBuilder; } public function buildForm(){        $this->formBuilder->addFormPart('<table>');      $this->formBuilder->addFormPart('<tr><td>First
    Name</td><td>');       $this->formBuilder->addElement
    ('text',array('name'=>'fname'));        $this->formBuilder->addFormPart('</tr>');       $this->formBuilder->addFormPart('<tr><td>Last
    Name</td><td>');         $this->formBuilder->addElement('text',array
    ('name'=>'lname'));        $this->formBuilder->addFormPart('</tr>'); $this->formBuilder->addFormPart
    ('<tr><td>Email</td><td>');        $this->formBuilder->addElement
    ('text',array('name'=>'email'));        $this->formBuilder->addFormPart('</tr>');        $this->formBuilder->addFormPart
    ('<tr><td>Comments</td><td>'); $this->formBuilder->addElement
    ('textarea',array('name'=>'comments','rows'=>'10','cols'=>'20'));         $this->formBuilder->addFormPart('</tr>');      $this->formBuilder->addFormPart
    ('<tr><td>&nbsp;</td><td>');         $this->formBuilder->addElement
    ('submit',array('name'=>'send','value'=>'Send Data'));        $this->formBuilder->addFormPart('</tr></table>'); } public function getForm(){ return $this->formBuilder->getForm(); } }

    Indeed, this is the point where things get really exciting! Notice how the above "FormDirector" class first takes up the respective builder object via its constructor, and second indicates to it how the online form must be constructed. The "buildForm()" method reflects this process in a nutshell, since it shows how the different controls that comprise the web form are added during the complete creation procedure. Here you can see that being the boss has some advantages!

    Now that you know how the long-awaited form director class looks, it's time to see how all the previous classes work together for creating a simple contact form. Here is a simple script that implements the builder pattern and displays the referenced online form:

    try{ // instantiate 'FormBuilder' object $formBuilder=new FormBuilder(); // instantiate 'FormDirector' object $formDirector=new FormDirector($formBuilder); // build web form    $formDirector->buildForm(); // display web form echo $formDirector->getForm(); } catch(Exception $e){ echo $e->displayMessage(); exit(); }

    The script shown above demonstrates with a few lines of code how the builder pattern works. In this example, first a form builder object is instantiated and then it is passed to the respective form director, which uses its "buildForm()" method to build the sample contact form.

    As you'll realize, the entire process aimed at displaying the mentioned form has been simplified to working with only two objects in conjunction with a few method calls. After studying the prior snippet, you'll agree with me that the builder pattern is actually useful!

    Final thought

    In this series, I went through the implementation of the builder pattern with PHP 5. During this set of three tutorials, hopefully you learned a few more things on pattern-based web programming, particularly in the terrain of creational patterns. Whether you're starting to use design patterns with your web applications or you're an experienced developer, I hope this series helped to expand your existing background in PHP.

    See you in the next PHP tutorial!



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek