PHP
  Home arrow PHP arrow Page 2 - Introducing the Composite Pattern 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 the Composite Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-03-07


    Table of Contents:
  • Introducing the Composite Pattern in PHP 5
  • Introducing the basics of the composite pattern
  • Implementing the composite pattern's model
  • Seeing the composite pattern in action

  • 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 the Composite Pattern in PHP 5 - Introducing the basics of the composite pattern
    ( Page 2 of 4 )

    In accordance with the concepts that I deployed in the beginning, you'll certainly recall that when the composite pattern is implemented, one specific object, or a set of them, expose an identical behavior across a given application. Even though this definition seems apparently complex, it must be admitted that translating it to functional PHP code is in fact much simpler than you might think.

    Therefore, I'm going to explain how this pattern works by building an abstract PHP class, which as you'll see in a few moments, will define the generic behavior for all the child objects created from it. Logically, in accordance with the schema established by the composite pattern, one or a group of objects that belong to this class will behave similarly in the context of a PHP application.

    Having said that, here is the basic structure corresponding to the aforementioned abstract class:   

    // define abstract 'FileInfoReader' class
    abstract class FileInfoReader{
       // get file info
       abstract public function getSelectedFileInfo(
    $singleFileReader);
       // get number of files
       abstract public function getNumberOfFileReaders();
       // add new file
       abstract public function addFileReader($singleFileReader);
    }

    As you can see, all that I did above was define a simple abstract PHP class, which exposes some generic methods for retrieving information on a given file, as well as for getting and adding a few file reader objects.

    Also, it's worthwhile to mention here that you shouldn't worry for the moment about how these objects will look, since their corresponding definition will be shown in the next section. Now, and returning to the signature of the previous abstract "FileInfoReader" class, you'll see that it presents a special structure. It has to be the model for creating objects that will behave similarly, whether a particular application uses one or a group of them. Sounds really interesting, right?

    But the question that comes up here is: how can this be achieved with PHP? Well, to be frank, the process is quite straightforward and will be limited to deriving two subclasses from the previous parent.

    In this case, the first child class will define the structure of one file reader, while the second one will set up the signature for multiple file readers, in this way implementing the schema dictated by the composite design pattern.

    Want to see how these brand new subclasses will be defined? Okay, go ahead and read the following section.



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