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

Developing a Form Director Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      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


    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this final part of the series, you'll learn how to build a form director class,...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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