Home arrow PHP arrow Page 4 - Developing a Form Director Class

Defining a form director class - PHP

Are you interested in expanding your knowledge of creational design patterns and learning how to work with director and builder objects in PHP 5? If your answer is yes, then I think you should start reading this article right now! Welcome to the concluding part of the series “How to Use Builder Objects in PHP 5.” This series leads you through the application of the builder pattern in PHP, and shows you how to use it with copious hands-on examples.

  1. Developing a Form Director Class
  2. Refreshing some core concepts
  3. The form builder class
  4. Defining a form director class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
October 18, 2006

print this article



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: