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

Review: the previous class loader program - 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).

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

TOOLS YOU CAN USE

advertisement

It's possible that you still haven't had an opportunity to look at the penultimate article of this series, where I discussed the development of a class loader program that could recursively seek a specified class through the file system on the web server, starting from a supplied path.

Thus, taking this possibility into account, below I listed the entire source code of this sample application, along with an example that shows how to use it in a concrete case.

First, here's the signature of the class that composes the loader program:

// register autoload() method of Autoloader class

 

spl_autoload_register('Autoloader::autoload');

 

// define Autoloader class

class Autoloader

{

public function __construct(){}

 

// autoload specified file

public static function autoload($file)

{

// the path should be defined as a constant or similar

$path = $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/';

$filepath = $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/' . $file . '.php';

if (file_exists($filepath))

{

require_once($filepath);

}

else

{

Autoloader::recursive_autoload($file, $path);

}

}

 

// try to load recursively the specified file

public static function recursive_autoload($file, $path)

{

if (FALSE !== ($handle = opendir($path)))

{

// search recursively the specified file

while (FAlSE !== ($dir = readdir($handle)))

{

if (strpos($dir, '.') === FALSE)

{

$path .= '/' . $dir;

$filepath = $path . '/' . $file . '.php';

if (file_exists($filepath))

{

require_once($filepath);

break;

}

Autoloader::recursive_autoload($file, $path);

}

}

closedir($handle);

}

}

}

With the definition of the above "Autoloader" class available, it's time to create another one, called "User," which will be used for testing purposes. Here's how it looks:

class User

{

 private $name = 'Alejandro';

private $email = 'alejandro@domain.com';

 

// constructor

public function __construct($name = '', $email = '')

{

if ($name != '')

{

$this->name = $name;

 

}

if ($email != '')

{

$this->email = $email;

}

}

 

// get user name

public function get_name()

{

return $this->name;

}

 

// get user's email address

public function get_email()

{

return $this->email;

}

 

function __toString()

{

return 'Name: ' . $this->name . ' Email: ' . $this->email;

}

}

Nothing unexpected, right? The definition of the previous "User" class is so simple to understand that it doesn't bear any further explanation. So, it's time to see how the "Autoloader" class can be used for creating a new user object and displaying some user-related data.

Here's the short script that performs this task in four lines of code:

// create instance of User class

$user = new User();

// display user data

echo $user;

There you have it. As long as the "User" class is located at the specified web server's directory or below, the "Autoloader" class will seek it recursively and include it in the calling script.

So far, everything looks good. However, as I expressed in the introduction, it's possible to improve the implementation of its "autoload()" method by using the directory iterator class that comes bundled with the Standard PHP Library, which permits you to traverse folders and subfolders in a true painless fashion.

Therefore, assuming that you're interested in learning how to use a directory iterator to make the previous "Autoloader" class more efficient, in the section to come I'm going to refactor the "autoload()" method by using this native PHP 5 class.

Now, 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
   

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: