HomePHP Page 5 - Introducing Builder Objects in PHP 5
Putting all the classes to work together - 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.
As I stated before, below I’ve coded a short script which demonstrates how to use the pertinent director and builder classes to generate a basic XML document. Please look at the following example:
Although the above script may seen rather primitive at first glance, indeed it reveals the real power that stands behind the builder pattern. First, a new “XMLBuilder” object is instantiated, which is passed directly to the constructor of the respective “XMLDirector” class. Finally, this uses its own methods to indicate to the builder how an XML page must be created, rendering the following XML document:
<?xml version="1.0" encoding="iso-8859-1"?>
<data>
<node>This is node 1</node>
<node>This is node 2</node>
<node>This is node 3</node>
<node>This is node 4</node>
<node>This is node 5</node>
<node>This is node 6</node>
</data>
Additionally, since I want you to have all the classes that I created previously available in one place, below I listed their corresponding definitions:
// define abstract 'AbstractXMLBuilder' class
abstract class AbstractXMLBuilder{
abstract function getXMLPage();
}
// define abstract 'AbstractXMLDirector' class
abstract class AbstractXMLDirector{
abstract function __construct(AbstractXMLBuilder $xmlBuilder);
abstract function buildXMLPage();
abstract function getXMLPage();
}
// define 'XMLPage' class
class XMLPage{
private $nodes=array();
public function addXMLNode($nodeValue='defaultValue'){
$this->nodes[]=$nodeValue;
}
// create source code for XML page
public function getXMLPage(){
$xml='<?xml version="1.0"
encoding="iso-8859-1"?>'."n".'<data>'."n";
foreach($this->nodes as $node){
$xml.='<node>'.$node.'</node>'."n";
}
$xml.='</data>';
return $xml;
}
}
// define concrete 'XMLBuilder' class
class XMLBuilder extends AbstractXMLBuilder{
private $xmlPage;
public function __construct(){
$this->xmlPage=new XMLPage();
}
// add new XML node
public function addXMLNode($nodeValue){
$this->xmlPage->addXMLNode($nodeValue);
}
// get source code of XML page
public function getXMLPage(){
return $this->xmlPage->getXMLPage();
}
}
// 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();
}
}
Definitely, after studying the above classes, you’ll agree with me that defining directors and builders with PHP is a no-brainer process!
Wrapping up
In this first article of the series, I walked you through the basics of creating directors and builder objects with PHP 5. I hope that all the examples you learned here will start you quickly on using the builder pattern in your own PHP projects.
In the next article, I’ll demonstrate how to use directors and builders in a more useful fashion: generating online forms. Want to see how this will be done? Read the next part!