HomePHP Page 4 - Introducing Builder Objects in PHP 5
Taking control of the process: a director class - PHP
In this article, the first of a three-part series, you will be introduced to the basics of creating directors and builder objects with PHP 5. As usual, there will be copious examples to help you quickly start using the builder pattern in your own PHP projects.
Defining a director class is a procedure closely similar to the one shown when creating the respective builder, therefore I think you should grasp the concept very quickly. As I did before, I’ll begin by defining an abstract class, which will set up the generic structure for all the eventual director objects.
Having said that, here is the signature of the brand new “AbstractXMLDirector” class. Please take a look at it:
// define abstract 'AbstractXMLDirector' class
abstract class AbstractXMLDirector{
abstract function __construct(AbstractXMLBuilder $xmlBuilder);
abstract function buildXMLPage();
abstract function getXMLPage();
}
As you can see, the above abstract class simply establishes the skeleton of all the objects that will be created from it. Notice the declaration of two primary methods aside from the respective constructor, called “buildXMLPage()” and “getXMLPage()” respectively.
In short, these will be responsible for instructing the previous builder about the way that all the XML pages should be created. Now, do you see why this class has been called the "director?" Certainly, it’s a faithful representation with PHP code of your own boss.
Now, leaving the jokes apart, please take a look at the following class’ source code, which shows a concrete implementation for the prior “AbstractXMLDirector” class:
// define concrete 'XMLDirector' class
class XMLDirector extends AbstractXMLDirector{
// accept 'XMLBuilder'object
public function __construct(AbstractXMLBuilder $xmlBuilder){
$this->xmlBuilder=$xmlBuilder;
}
// build XML page
public function buildXMLPage(){
$this->xmlBuilder->addXMLNode('This is node 1');
$this->xmlBuilder->addXMLNode('This is node 2');
$this->xmlBuilder->addXMLNode('This is node 3');
$this->xmlBuilder->addXMLNode('This is node 4');
$this->xmlBuilder->addXMLNode('This is node 5');
$this->xmlBuilder->addXMLNode('This is node 6');
}
// return XML page to calling code
public function getXMLPage(){
header('Content-Type:text/xml');
return $this->xmlBuilder->getXMLPage();
}
}
As shown above, now the brand new “XMLDirector” class exposes the corresponding concrete methods to instruct the builder in question on how all the XML pages must be created. In addition, it should be noted how the builder object itself is fed straight into the constructor, which comes in handy for using all its methods.
Al right, after understanding how the previous director class works, I guess that it’s also easy seeing how the builder pattern can be used for building target objects. In this case, the appropriate combination of the director and builder objects respectively results in the adequate creation of XML pages.
However, before you finish reading this tutorial, I’d like to show you how all the classes that I defined previously can be used together to generate programmatically some XML pages. Therefore, in the next few lines, you’ll see the builder pattern in action. Thus I suggest you to jump straight into the following section. I’ll be there, waiting for you.