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

  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



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){
// 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(){
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.

>>> 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: