HomePHP Page 3 - Introducing Builder Objects in PHP 5
Of builders and XML pages: the programmatic creation process - 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 you learned in the course of the previous section, the “XMLPage” class was tasked with creating basic XML documents. Aside from showing you its rather limited functionality, I want you to understand how a director and a builder together can fit into the whole picture.
First off, let me demonstrate the complete process for defining a builder object, to give you a better idea of how it works with reference to the prior example. Essentially, if I want to create a builder class, which will be directly responsible for the correct creation of different XML pages, I might define the following class:
// define abstract 'AbstractXMLBuilder' class
abstract class AbstractXMLBuilder{
abstract function getXMLPage();
}
As you can see, what I did above was simply define the general structure of an XML builder object that has a unique method called “getXMLPage().” Of course, the point of creating the above abstract class is merely to establish the generic signature of all the eventual objects that belong to the “AbstractXMLBuilder” class.
Still with me? Fine, now let me go one step further and specify a concrete implementation for the abstract builder class that you saw before. Based upon its structure, its concrete version would look like this:
// 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();
}
}
Now, things should be much clearer to you, since the above class offers a concrete implementation for the “getXMLPage()” method that was declared previously. In addition, I’d like to stress how the corresponding constructor performs the instantiation of the target object, in this case referenced as “XMLPage,” to use all its methods for constructing the pertinent XML document. That was simple to learn, wasn’t it?
Okay, at this stage you have hopefully grasped all the concepts behind creating a builder class. As you saw, all the mentioned class does is take up an object of type “XMLPage” and use its methods to render a basic XML document. However, when it comes to applying the builder pattern, I said initially that there were two objects involved in the complete creation process. Since you already learned now to create a builder, the missing piece here is obviously the director!
Basically, this second object will indicate programmatically to the builder the manner in which the respective XML pages must be generated, completing the creation procedure that I discussed before, based on two primary objects. Do you see now how the pieces start fitting together?
Now that you understand how a builder class can be defined with PHP, let’s move forward and see how the corresponding director can be created. To learn how this will be achieved, you must click on the link below and keep reading.