Home arrow PHP arrow Page 2 - Using Directory Iterators and MySQL with Adapter Objects with PHP

Working with directory iterators and adapters together - PHP

If you’re a PHP programmer who’s searching for a comprehensive tutorial on how to create adapter classes with PHP 5, them look no further. Welcome to the final installment of the series “Implementing adapter objects with PHP.” Made up of two parts, this series teaches you how to implement the adapter design pattern in PHP 5, and it accompanies the corresponding theory with educational examples.

TABLE OF CONTENTS:
  1. Using Directory Iterators and MySQL with Adapter Objects with PHP
  2. Working with directory iterators and adapters together
  3. Putting the adapter class to work
  4. Adapting MySQL result sets
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 13, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The first example that I plan to develop in this article is focused on creating an adapter class which will eventually expand the functionality of the popular "DirectoryIterator" class included with the SPL package. Additionally, even when this approach may not be the best one (inheritance is preferred for this specific case), it will allow me to demonstrate how to use the adapter pattern in a more useful situation.

Having clarified the above issue, take a look at the following code snippet, which defines the pair of classes that comprise the adapter entity discussed earlier:

//define abstract 'AbstractDirectoryIteratorAdapter' class
abstract class AbstractDirectoryIteratorAdapter{
   private $dirIterator;
    abstract public function getDetailedInfo();
}
//define concrete 'DirectoryIteratorAdapter' class
class DirectoryIteratorAdapter extends AbstractDirectoryIteratorAdapter{
   public function__construct(DirectoryIterator $dirIterator){
       
$this->dirIterator=$dirIterator;
    }
    public function getDetailedInfo(){
        $info='';
   
foreach($this->dirIterator as $dirEntry){
            $info.=str_repeat('-',80).'<br/>';
            $info.='Last modification timestamp of current directory 
entry is :'.$dirEntry->getMTime().'<br />';             $info.='Path of current directory entry is :
'.$dirEntry->getPath().'<br />';             $info.='Type of current directory  entry is :
'.$dirEntry->getType().'<br />';             $info.='Full path of current directory entry is :
'.$dirEntry->getPathName().'<br/>';             $info.=$dirEntry->isReadable()?'Current directory entry is
readable<br />':'Current directory entry is not readable<br />';         }         return $info;              } }

If you take some time and examine the above example, you'll realize that there are some interesting aspects worthy of highlighting here. First, notice how the constructor that corresponds to this new "DirectoryIteratorAdapter" class takes up an object of type "DirectoryIterator," which is assigned opportunely as a class property.

As you can see, the reason for doing this is really obvious. Since I'm not using inheritance in this case, the simplest way to expand the functionality of a specific class is by simply injecting the class in question straight into its adapter. That was simple, wasn't it?

Second, the previous adapter class also exposes the "getDetailedInfo()" method, which uses three predefined methods that come with "DirectoryIterator" to expand its original functionality and provide more specific information about a given directory and its corresponding entries.

In addition, I'd like to note that I used the conventional approach for concatenating strings inside this referenced method, but you can easily change this and utilize the "heredoc" syntax, which is slightly more understandable.

At this stage, I'm pretty certain that you grasped the technique that I applied here for creating an adapter class, aimed at extending the functionality of the familiar "DirectoryIterator." However, there is still more material to learn, since I want to show you how this adapter class works with a concrete example.

Sounds really intriguing, right? Then, don't waste any more time; read the following section.



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