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.
blog comments powered by Disqus |
|
|
|
|
|
|
|