Home arrow PHP arrow Page 4 - Working with Directory Iterators and Proxy Classes with PHP 5

Completing the proxy pattern - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the section that you just read, in order to complete the programmatic model imposed by the proxy pattern, I need to define the signature that corresponds to the "DirectoryProcessor" class. As you just learned, this class is used by the aforementioned proxy.

Since my intention here is to demonstrate how a proxy class can be coupled with a directory iterator, the directory process that I plan to build will consist of a simple wrapper for the "DirectoryIterator" class that comes with PHP 5. It's just as simple as it sounds.

Having said that, here is the definition for this brand new class:

// define 'DirectoryProcessor' class class DirectoryProcessor{ private $dirProcessor; public function __construct($dirPath){ if(!$this->dirProcessor=new
DirectoryIterator($dirPath)){ throw new Exception('Error instantiating
directory processor object'); } } // get path of selected directory public function getPath(){ return $this->dirProcessor->getPath(); } // get size of directory entries public function getSize(){ $output='Sizes for directory entries are as
following:<br />'; foreach($this->dirProcessor as $dirElement){ $output.='Size for current element
is :'.$dirElement->getSize().'<br />'; } return $output; } // get names of directory entries public function getFileNames(){ $output='Names for directory entries are as
following:<br />'; foreach($this->dirProcessor as $dirElement){ if($dirElement->isFile()){ $output.='Name of current file
is : '.$dirElement->getFileName().'<br />'; } } return $output; } // get timestamps of directory entries public function getTimeStamps(){ $output='Timestamps for directory entries are as
following:<br />'; foreach($this->dirProcessor as $dirElement){ if($dirElement->isFile()){ $output.='Timestamp of current file
is : '.$dirElement->getMTime().'<br />'; }                  }                  return $output; } // get last access timestamps of directory entries public function getLastTimeStamps(){ $output='Last access timestamps for directory entries
are as following:<br />'; foreach($this->dirProcessor as $dirElement){ if($dirElement->isFile()){   $output.='Last access timestamp of current
file is : '.$dirElement->getATime().'<br />';                          }                         }                         return $output;             }             // get last access timestamps of inodes             public function getInodeLastTimeStamps(){ $output='Last inode modification timestamp for
directory entries are as following:<br />'; foreach($this->dirProcessor as $dirElement){                 if($dirElement->isFile()){                 $output.='Last inode modification timestamp of current
file is :'.$dirElement->getCTime().'<br />';                  }                  } return $output; }             // get directory processor object             public function getDirectoryProcessor(){ return $this; } }

Now, definitely things are getting really interesting! As you can see, the above "DirectoryProcessor" class behaves like a simple wrapping layer for the popular "DirectoryIterator" class that you've used many times with your PHP 5-based applications. In this case in particular, all the functionality provided by the original proxy class is implemented concretely by the corresponding iterator.

Now that you know how the previous directory processor class looks, it's time to jump forward and tackle the final section of the article, where I'll be demonstrating how the pair of neat classes that I created can be integrated in a single example.

Provided that you're interested in learning how these classes fit each other, go ahead and read the next few lines. 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: