Home arrow PHP arrow Page 3 - Controlling Online Forms with Builder Objects in PHP 5

Creating a form builder class - PHP

Mastering some of the most popular design patterns with PHP 5 can be sometimes an overwhelming process that requires hard work and experience. However, if you want to tackle the challenge and expand your existing background on them, this article might eventually find a place on your quick reference list.

  1. Controlling Online Forms with Builder Objects in PHP 5
  2. Defining a form element class
  3. Creating a form builder class
  4. Constructing a basic online form
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
October 11, 2006

print this article



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

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

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: