PHP
  Home arrow PHP arrow Page 3 - 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 - Of builders and XML pages: the programmatic creation process
    ( Page 3 of 5 )

    As you learned in the course of the previous section, the “XMLPage” class was tasked with creating basic XML documents. Aside from showing you its rather limited functionality, I want you to understand how a director and a builder together can fit into the whole picture.

    First off, let me demonstrate the complete process for defining a builder object, to give you a better idea of how it works with reference to the prior example. Essentially, if I want to create a builder class, which will be directly responsible for the correct creation of different XML pages, I might define the following class:

    // define abstract 'AbstractXMLBuilder' class
    
    abstract class AbstractXMLBuilder{
    
    	abstract function getXMLPage();
    
    }
    

    As you can see, what I did above was simply define the general structure of an XML builder object that has a unique method called “getXMLPage().” Of course, the point of creating the above abstract class is merely to establish the generic signature of all the eventual objects that belong to the “AbstractXMLBuilder” class.

    Still with me? Fine, now let me go one step further and specify a concrete implementation for the abstract builder class that you saw before. Based upon its structure, its concrete version would look like this:

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

    Now, things should be much clearer to you, since the above class offers a concrete implementation for the “getXMLPage()” method that was declared previously. In addition, I’d like to stress how the corresponding constructor performs the instantiation of the target object, in this case referenced as “XMLPage,” to use all its methods for constructing the pertinent XML document. That was simple to learn, wasn’t it?

    Okay, at this stage you have hopefully grasped all the concepts behind creating a builder class. As you saw, all the mentioned class does is take up an object of type “XMLPage” and use its methods to render a basic XML document. However, when it comes to applying the builder pattern, I said initially that there were two objects involved in the complete creation process. Since you already learned now to create a builder, the missing piece here is obviously the director!

    Basically, this second object will indicate programmatically to the builder the manner in which the respective XML pages must be generated, completing the creation procedure that I discussed before, based on two primary objects. Do you see now how the pieces start fitting together?

    Now that you understand how a builder class can be defined with PHP, let’s move forward and see how the corresponding director can be created. To learn how this will be achieved, you must click on the link below and keep reading.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT