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

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

try{
// instantiate 'XMLBuilder' object
$xmlBuilder=new XMLBuilder();
// instantiate 'XMLDirector' object
$xmlDirector=new XMLDirector($xmlBuilder);
// build XML page
  
$xmlDirector->buildXMLPage();
// display XML page
echo $xmlDirector->getXMLPage();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

 

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!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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