PHP
  Home arrow PHP arrow Page 5 - Introducing Builder Objects in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introducing Builder Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-10-04


    Table of Contents:
  • Introducing Builder Objects in PHP 5
  • Building basic XML documents: definition of the target object
  • Of builders and XML pages: the programmatic creation process
  • Taking control of the process: a director class
  • Putting all the classes to work together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introducing Builder Objects in PHP 5 - Putting all the classes to work together
    ( Page 5 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT