PHP
  Home arrow PHP arrow Page 2 - 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 - Building basic XML documents: definition of the target object
    ( Page 2 of 5 )

    After reading the article’s introduction, quite possibly you're still wondering whether it is possible to involve two specific objects in creating a third one which is responsible for performing a particular task. Fortunately for you and me, the answer is a resounding yes.

    By applying the concept of directors and builders, it’s feasible to build another object in such a way that the entire creation process can be conducted in accordance with the orders (or directives) dictated by a director.

    I know that all this may sound fairly complicated, at least when analyzed from the raw theory, but things can be more complex, particularly if you want to translate the definitions of directors and builders to functional PHP code. Precisely for that reason, since I want you to learn how to include these types of objects inside your own PHP applications, I’ll set up an educational example. It will show you how to apply the builder pattern quickly.

    To begin, suppose you need to define a PHP class and give it the ability to construct basic XML documents. Given that idea, the structure of the mentioned class might look like this:

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

     

    As you can see, the above defined “XMLPage” class has been provided with the capacity to construct simple XML documents. This is convenient for demonstrating how the builder pattern can work. With reference to the previous class, you'll probably realize that the method listed below:

     

    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;
    }
    

     

    is the real workhorse for creating a concrete XML page, since it returns to the calling code the corresponding XML output. Also, it should be noticed that the “XMLPage” class has another handy method, called “addXMLNode()” that allows you to add new nodes dynamically to the current XML document.

    So far, the previously listed class should be fairly easy to understand since its definition is comprehensive. However, there isn’t yet a clear indication of how two additional PHP objects, that is a director and a builder respectively, can be used together to define programmatically how each XML page should be created. However, fear not since this process is about to be revealed in the following section!

    Therefore, if you’re intrigued by learning how directors and builders can be created with PHP, keep reading. Trust me, things are getting really exciting now!



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek