Home arrow PHP arrow Page 3 - The Basics of Implementing Adapter Objects with PHP

Defining a concrete directory processor class - PHP

You may have already encountered situations in coding PHP applications in which you would like to use inheritance, but could not for one reason or another. Fortunately, there is an alternative for these situations that can help you achieve the same result. In this first article in a two-part series, you'll learn about the adapter pattern.

TABLE OF CONTENTS:
  1. The Basics of Implementing Adapter Objects with PHP
  2. The basics of adapter objects
  3. Defining a concrete directory processor class
  4. Seeing the adapter pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
December 06, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: