HomePHP Page 2 - Introducing Builder Objects in PHP 5
Building basic XML documents: definition of the target object - 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.
After reading the article’s introduction, quite possibly you're still wondering whether it is possible to involve two specific objects in creating a third one which is responsible for performing a particular task. Fortunately for you and me, the answer is a resounding yes.
By applying the concept of directors and builders, it’s feasible to build another object in such a way that the entire creation process can be conducted in accordance with the orders (or directives) dictated by a director.
I know that all this may sound fairly complicated, at least when analyzed from the raw theory, but things can be more complex, particularly if you want to translate the definitions of directors and builders to functional PHP code. Precisely for that reason, since I want you to learn how to include these types of objects inside your own PHP applications, I’ll set up an educational example. It will show you how to apply the builder pattern quickly.
To begin, suppose you need to define a PHP class and give it the ability to construct basic XML documents. Given that idea, the structure of the mentioned class might look like this:
// 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;
}
}
As you can see, the above defined “XMLPage” class has been provided with the capacity to construct simple XML documents. This is convenient for demonstrating how the builder pattern can work. With reference to the previous class, you'll probably realize that the method listed below:
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;
}
is the real workhorse for creating a concrete XML page, since it returns to the calling code the corresponding XML output. Also, it should be noticed that the “XMLPage” class has another handy method, called “addXMLNode()” that allows you to add new nodes dynamically to the current XML document.
So far, the previously listed class should be fairly easy to understand since its definition is comprehensive. However, there isn’t yet a clear indication of how two additional PHP objects, that is a director and a builder respectively, can be used together to define programmatically how each XML page should be created. However, fear not since this process is about to be revealed in the following section!
Therefore, if you’re intrigued by learning how directors and builders can be created with PHP, keep reading. Trust me, things are getting really exciting now!