Since my plan here is to put all of the registries to work together, I'm going to define a basic autoloader class that will include all of these source classes behind the scenes, on request. Having said that, here's the source code corresponding to the aforementioned autoloader: (Autoloader.php) <?php class Autoloader { private static $_instance; // get Singleton instance of the autoloader public static function getInstance() { if (!self::$_instance) { self::$_instance = new self; } return self::$_instance; } // private constructor private function __construct() { spl_autoload_register(array('Autoloader', 'autoload')); } // prevent cloning instance of the autoloader private function __clone(){} // autoload a class on demand public static function autoload($class) { $file = $class . '.php'; if (!file_exists($file)) { require_once 'ClassNotFoundException.php'; throw new ClassNotFoundException('The file containing the requested class was not found.'); } include $file; unset($file); if (!class_exists($class, false)) { require_once 'ClassNotFoundException.php'; throw new ClassNotFoundException('The requested class was not found.'); } } } (ClassNotFoundException.php) <?php class ClassNotFoundException extends Exception{} If you're used to taking advantage of the built-in autoloading capabilities offered by PHP, then the definition of the above class should be quite familiar to you. All it does is register its static "autoload()" method in the SPL stack, to include classes on demand. In addition, the class throws a couple of custom exceptions at the proper places, but this feature can be modified or even removed. All right, having already defined a basic autoloader that include classes on request, the last step that must be taken is to set up a script that puts all the registries to work together. This will be done in the final section of this tutorial, so click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|