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.
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> </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.