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

The form builder 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In order to get the schema imposed by the builder pattern nearer to completion, it's convenient to recall quickly the signature of the respective "FormBuilder" class, along with its corresponding abstract parent.

Below I have listed the full source code that corresponds to these two classes:

// define abstract 'AbstractFormBuilder' class abstract class AbstractFormBuilder{ abstract function getForm(); } // define concrete 'FormBuilder' class class FormBuilder extends AbstractFormBuilder{ private $html=array(); private $action; private $method; public function __construct($action='',$method='post'){ // setup form attribute        $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; } }

All right, now I firmly believe that the two classes listed above are fresh in your mind. If they aren't yet, let me explain briefly how they fit into the builder pattern schema. As you'll see, the prior "FormBuilder" class is tasked with generating the whole online form, in accordance with some input parameters passed to its constructor, such as the $method and $action variables.

In addition, the class also comes with the "addElement()" method, which is pretty handy for adding different form elements to the general structure. In this way it allows for the dynamic generation of regular online forms. Of course, the remaining class methods speak for themselves, therefore I'll skip rapidly over them.

At this stage, and based on the set of classes that were listed earlier, the schema established by the pattern builder is very close to completion. After all, I have on one hand a "FormElement" class (in this case referenced as the target element) that obviously renders different form controls, while on the other hand there's the form builder class that you saw before, responsible for using the mentioned form elements to create the complete online form.

So, the question that comes up now is, what's the missing piece here? Obviously, the element that remains undefined is a form director class, which will instruct to the form builder on how the online form in question must be constructed. Indeed, that makes a lot of sense!

Regarding the definition of the corresponding form director class, in the upcoming section I'll show you its complete signature. If you want to see how this brand new class looks, please keep reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: