PHP
  Home arrow PHP arrow Page 3 - The Basics of Implementing Adapter Obj...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

The Basics of Implementing Adapter Objects with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Over the course of this first tutorial, you'll learn the foundations on how to...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway