PHP
  Home arrow PHP arrow Page 3 - 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 - Defining a concrete directory processor class
    ( Page 3 of 4 )

    As I expressed in the section that you just read, the next step involved in the creation of an adapter class consists of defining a concrete directory processor which will implement all the abstract methods that were defined in the respective base class. Sounds logical, right?

    Okay, having explained that, below I defined the structure of the brand-new “DirectoryProcessor” class that I mentioned before. Please take a look:

    // define concrete 'DirectoryProcessor' class class DirectoryProcessor extends AbstractDirectoryProcessor{ //define constructor           public function __construct($dirPath){                 if(!is_dir($dirPath)){                         throw new Exception('Invalid directory path!'); }                   $this->dirPath=$dirPath;           }          // implement concretely 'fetchDirContent()' method             public function fetchDirContent(){                     if(!$dp=opendir($this->dirPath)){                     throw new Exception('Error opening selected
    directory!');                   }                    $dircont='';                    while($file=readdir($dp)){                            $dircont.=$file.'<br />';                     }                     fclose($dp);                    // return directory contents                   return $dircont;            }             // implement concretely 'getDirInfo()' method             public function getDirInfo(){                     $pathinfo=pathinfo($this->dirPath);                    return 'Name of selected directory is
    '.$pathinfo['dirname'].' and its base name is the following:  '.$pathinfo['basename'];

          } }

     

    As you can see, the above defined “DirectoryProcessor” class simply implements in a concrete way all the methods that were originally declared abstract in the corresponding base class. In addition, the specific methods exposed by this new class perform some simple tasks, like fetching the content of a specified directory, which is directly inputted via the constructor, as well obtaining its name and path.

    So far, I have at disposal a fully-functional directory handling class, which can be basically used as indicated below:

    try{ // instantiate 'DirectoryProcessor' object        $dirProc=new DirectoryProcessor('default_path/');         // display contents of directory         echo '<h2>Contents of selected directory are as follows:</h2>'; echo $dirProc->fetchDirContent();            // display information on selected directory         echo '<h2>Information on selected directory is as follows:</h2>'; echo $dirProc->getDirInfo(); } catch(Exception $e){         echo $e->getMessage();         exit(); } /* displays the following: Contents of selected directory are as follows: . .. file1.txt file2.txt Information on selected directory is as follows: Name of selected directory is . and its basename is the following:
    default_path */

    In this case, provided that the sample “default_path” directory above specified contains the files “file1.txt” and “file2.txt” respectively, the output generated by the previous script corresponds to a logical expectation.

    However, there are two important things worth noticing about the previous example. Obviously, the first one rests on the fact that the same functionality could have been achieved using the “DirectoryIterator” class that comes with PHP 5, while the second one is even more relevant for this article in particular, since there’s still not a clue of how to create an adapter class.

    Well, let me answer these questions in the correct order: definitely I could have used the neat “DirectoryIterator” class bundled with the SPL package to work with a highly standard directory handling class, but you shouldn’t worry about this for the moment, since this procedure will be covered in the following article of this series.

    And finally, with reference to the second question, here is where things get really interesting!

    Suppose that for whatever reason, you need to expand the functionality of the previous “DirectoryProcessor” class, but without using the goodies of inheritance, in addition to giving the class a predefined structure. How can this be done? This issue can be easily tackled by creating an adapter class that performs the mentioned expansion!

    Want to learn how this adapter class will be defined? Go ahead and visit 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