PHP
  Home arrow PHP arrow Page 4 - The Basics of Implementing Adapter Objects with PHP
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

The Basics of Implementing Adapter Objects with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-12-06


    Table of Contents:
  • The Basics of Implementing Adapter Objects with PHP
  • The basics of adapter objects
  • Defining a concrete directory processor class
  • Seeing the adapter 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


    The Basics of Implementing Adapter Objects with PHP - Seeing the adapter pattern in action
    ( Page 4 of 4 )

    As discussed in the previous section, one of the easiest ways to extend the functionality of a given class without having to use inheritance is simply by defining an adapter class. The adapter class will modify one or more methods of the original one to conform a specific model.

    This being said, and based upon all the prior sample classes, here is the complete process for creating the aforementioned adapter class. Take a look, please:

    // define abstract 'DirectoryProcessorAdapter' class abstract class AbstractDirectoryProcessorAdapter{       private $dirProcessor;       abstract public function getDetailedDirInfo(); } // define concrete 'DirectoryProcessorAdapter' class class DirectoryProcessorAdapter extends AbstractDirectoryProcessorAdapter{             // define constructor             public function __construct(DirectoryProcessor $dirProcessor){                         $this->dirProcessor=$dirProcessor;             }             // implements concretely 'fetchDetailedDirContent()' method             public function getDetailedDirInfo(){                         return '<h1>Detailed information about selected
    directory is as follows:</h1><h2>Contents of selected directory is as follows:</h2>'.$this->dirProcessor->
    fetchDirContent().'<h2>Data about selected directory is as follows:</h2>'.$this->dirProcessor->getDirInfo();             } }

    As you can see, the above defined “DirectoryProcessorAdapter” class is now capable of extending the functionality of the previous “DirectoryProcessor” by simply injecting this object into the corresponding constructor, and finally defining the completely new “fetchDetailedDirContent()” method.

    Here, it’s clear to see that this method combines the capacity of “fetchDirContent()” and “getDirInfo()” in one single block and displays the respective information on the browser. See how an adapter class now fits into the whole picture?

    Now that you hopefully grasped the logic that drives the “DirectoryProcessorAdapter” class that you learned before, I’d like to go one step forward and show you how to properly use this class. Therefore, examine the example listed below, which demonstrates a simple implementation of the adapter class:

    try{ // instantiate 'DirectoryProcessor' object         $dirProc=new DirectoryProcessor('default_path/');         // instantiate 'DirectoryProcessorAdapter' object         $dirProcAdapter=new DirectoryProcessorAdapter($dirProc); // display detailed information on selected director
    (implements the adapter pattern)         echo $dirProcAdapter->getDetailedDirInfo(); } catch(Exception $e){     echo $e->getMessage();     exit(); } /* displays the following: Detailed information about selected directory is as follows: Contents of selected directory is as follows: . .. file1.txt file2.txt Data about selected directory is as follows: Name of selected directory is . and its basename is the following:
    default_path */

    As you’ll realize, the previous example demonstrates how easy it is to use the adapter class that I defined a few lines above. After a new directory processor object and its respective adapter have been instantiated, the “getDetailedDirInfo()” method is called up at the end of the script, which neatly displays the directory information detailed above. 

    After all, implementing the adapter pattern with PHP 5 isn’t difficult. As you saw in the previous example, a given class can be easily modified without using inheritance, in cases where this approach is not viable.

    To wrap up

    We’ve come to the end of this first article. Hopefully, after learning all the concepts and examples given here, you’ll have a clearer idea of how to create an adapter class with PHP.

    But this educational journey hasn’t ended yet, since in the second (and last) tutorial you’ll learn how to create an adapter that works in tandem with the standard “DirectoryIterator” class, included with PHP 5, in addition to building adapters for MySQL. Therefore, meet you in the last part!



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