As I said in the segment that you just read, the Standard PHP Library comes bundled with a handy function called “spl_autoload_register(),” which allows you to use a callback method or function for automatically loading classes. To illustrate how to use this function in more detail, below I defined another sample class called “Autoloader(),” whose “autoload()” method will be tied to the function in question. That being explained, here’s how to use this class in a concrete case: // example on using the spl_autoload_register() function // 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); } } } // create instance of User class (it's autoloaded by the Autoloader class) $user = new User(); // display user data echo $user; There you have it. As you can see, the “autoload()” method defined within the above class has been linked to the “spl_autoload_register()” function, meaning that this method will be called by the PHP parser when attempting to include a specific class. Also, the method is capable of performing recursive searches to find the class that needs to be included, as you can see clearly in the prior example. Not too hard to grasp, right? Finally, with this wrapping code sample I’m finishing this seventh part of the series. As always, feel free to edit and improve all of the examples shown in this article. This way you can arm yourself with a better background in building class loading programs in PHP 5. The experience will be extremely educational, believe me. Final thoughts In this seventh installment of the series, you learned how to use the “spl_autoload(),” “spl_register()” and “spl_register()” functions to build an small file loader class which was capable of performing recursive searches through the file system to find a targeted resource. In this particular case, the recursive algorithm used by the class was rather lengthy and hard to grasp. However, the Standard PHP library includes a set of iterators that allow you to traverse directories in a truly effortless fashion. So, in the last chapter of this series I’m going to incorporate an iterator into the previous loader class, making its source code a bit more compact and readable. Don’t miss the final tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|