Home arrow PHP arrow 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.

  1. Introducing Builder Objects in PHP 5
  2. Building basic XML documents: definition of the target object
  3. Of builders and XML pages: the programmatic creation process
  4. Taking control of the process: a director class
  5. Putting all the classes to work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
October 04, 2006

print this article



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"
foreach($this->nodes as $node){       
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"
foreach($this->nodes as $node){
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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: