Home arrow PHP arrow 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.

  1. Working with Directory Iterators and Proxy Classes with PHP 5
  2. Defining the initial structure of a proxy directory class
  3. Improving the functionality of the proxy class
  4. Completing the proxy pattern
  5. Developing a final example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
January 09, 2007

print this article



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. 

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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