HomePHP Page 3 - Using Directory Iterators and MySQL with Adapter Objects with PHP
Putting the adapter class to work - 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.
Naturally, I know that you want to see how the adapter class that I defined in the previous section works, therefore I prepared a concrete example that demonstrates clearly the functionality of this class.
In this case, and assuming there's a sample "default_path" directory that contains two files named "file1.txt" and "file2.txt," here's the corresponding code sample:
try{
$dirIteratorAdapter=new DirectoryIteratorAdapter(new
DirectoryIterator('default_path/'));
echo $dirIteratorAdapter->getDetailedInfo();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
/*
displays the following:
--------------------------------------------------------------------------
Last modification timestamp of current directory entry is : 1155050050
Path of current directory entry is : default_path
Type of current directory entry is : dir
Full path of current directory entry is :default_path/.
Current directory entry is readable
--------------------------------------------------------------------------
Last modification timestamp of current directory entry is : 1155061462
Path of current directory entry is : default_path
Type of current directory entry is : dir
Full path of current directory entry is :default_path/..
Current directory entry is readable
--------------------------------------------------------------------------
Last modification timestamp of current directory entry is : 1149698631
Path of current directory entry is : default_path
Type of current directory entry is : file
Full path of current directory entry is :default_path/file1.txt
Current directory entry is readable
--------------------------------------------------------------------------
Last modification timestamp of current directory entry is : 1149698651
Path of current directory entry is : default_path
Type of current directory entry is : file
Full path of current directory entry is :default_path/file2.txt
Current directory entry is readable
*/
As you can see, the prior example shows how the original "DirectoryIterator" class can be expanded without using inheritance. Also, with reference to the script listed above, you'll realize that it iterates over each directory entry and displays some useful information, such as the path of each element, type, timestamp, and so forth.
So far, with the example you saw before, I think you should be equipped with a fairly decent background on how to apply the adapter pattern with PHP 5. Nevertheless, I'd like to finish this series by teaching you another case where this pattern can be applied with minor hassles: creating an adapter that expands the capacity of a MySQL result set processing class.
Want to find out how this adapter class will be created? Jump into the following section and keep reading.