Home arrow PHP arrow Page 3 - Using Directory Iterators to Build Loader Apps in PHP

Improving the definition of the Autoloader class using directory iterators - PHP

Welcome to the final part of an eight-part series on building loader applications in PHP. In this part, we'll improve on the loader class we developed in the previous part by using certain specific functions from the Standard PHP Library (SPL).

  1. Using Directory Iterators to Build Loader Apps in PHP
  2. Review: the previous class loader program
  3. Improving the definition of the Autoloader class using directory iterators
  4. Putting the Autoloader class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
July 02, 2009

print this article



As you'll possibly recall, the "autoload()" method that belongs to the previous loader class performs the recursive search of a particular class via another method, not surprisingly called "recursive_autoload()." As I mentioned earlier, though, it's feasible to simplify this process by means of the RecursiveDirectoryIterator class that comes with the SPL library.

To demonstrate how this can be accomplished, below I included an improved version of the "Autoloader" class, this time using the directory iterator class. Here's how the class now looks:

spl_autoload_register(NULL, FALSE);


spl_autoload_register(array('Autoloader', 'autoload'));


// define Autoloader class

class Autoloader


// constructor not implemented

public function __construct(){}


// autoload recursively a specified class

public static function autoload($class)


// Set path for specified file

$class = strtolower($class) . '.php';

// try to include recursively the class file

$rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(BASEPATH));

foreach ($rit as $entry)


if ($class === $entry->getFileName())






throw new FileNotFoundException('File ' . $class . ' not found!');




// define custom FileNotFoundException exception class

class FileNotFoundException extends Exception


public function __construct($message)


parent:: __construct();




Definitely, things are getting much more interesting now. As you can see, the "autoload()" method defined above uses the functionality of the RecursiveDirectoryIterator native class to recursively search for a specified class through the file system. If the class in question is found, then it's included via a simple "require_once()" function. Otherwise, a custom "FileNotFoundException" exception is thrown and caught appropriately. Not too difficult to grasp, right?

From the code sample shown previously, it's obvious to see how useful the Standard PHP library can be for loading classes automatically and performing recursive searches.

However, the best way to understand how the enhanced version of the "Autoloader" class works is by means of an illustrative example. Therefore, in the last section of this article, I'm going to develop that example, in this case for loading the "User" class defined before.

Now, jump forward and read the next few lines. We're almost done!

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