PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Taking control of the process: a director class
    ( Page 4 of 5 )

    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){
         
    		$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();
    
    	}
    
    }
    

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek