HomePHP Page 2 - Working with Directory Iterators and Proxy Classes with PHP 5
Defining the initial structure of a proxy directory class - PHP
If you’re a strong advocate of using pattern-based programming for developing your PHP applications, then this pair of comprehensive tutorials might find a place on your quick reference list. Welcome to the concluding part of the series “Building Proxy Classes with PHP 5.” In two parts, this series introduces the foundations of how to apply the proxy pattern with PHP 5, and it accompanies the corresponding theory with extensive code samples.
In consonance with the concepts that I explained in the introduction, I'm going to demonstrate how to build a generic proxy class that can be easily coupled with a directory iterator. The result of this integration will allow you develop a simple application that collects information about a selected directory via a proxy object.
Since the challenge is really appealing, let me commence by showing you the initial signature for this new proxy class. Its source code is as follows:
// define 'DirectoryProcessor' class
class ProxyDirectoryProcessor{
private $dirPath;
private $dirProcessor=NULL;
public function __construct($dirPath='defaultPath'){
if(!is_dir($dirPath)){
throw new Exception('Invalid input directory');
}
$this->dirPath=$dirPath;
}
// get directory path
public function getPath(){
if($this->dirProcessor==NULL){
$this->createDirectoryProcessor();
}
return $this->dirProcessor->getPath();
}
//get size of directory entries
public function getSize(){
if($this->dirProcessor==NULL){
$this->createDirectoryProcessor();
}
return $this->dirProcessor->getSize();
}
//get names of directory entries
public function getFileNames(){
if($this->dirProcessor==NULL){
$this->createDirectoryProcessor();
}
return $this->dirProcessor->getFileNames();
}
}
If you're familiar with how the proxy pattern works, then you'll find the above class extremely easy to grasp. As you can see, the previous "ProxyDirectoryProcessor" class has some neat methods for retrieving some useful information about the entries included into a selected directory, such as their size, names and path respectively.
Also, you should noticed how the proxy class obtains all this data by using the group of methods provided by a directory processor object, which for the moment remains undefined. In addition, I'd like to stress one point that deserves special attention here: note how this directory processor isn't instantiated by the corresponding constructor; instead, the object in question is only created when its functionality is required. Pretty neat, right?
Okay, I think that the previous proxy class now gives you a clear idea of how it works, as well as how it can be easily coupled with a directory iterator. However, the functionality offered by this class is rather limited, so let me add some extra methods to it before I proceed to define the respective director processor class that you saw before.
Do you want to see how this will be done? Please click on the link below and keep reading.