Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AutoLoader
- {
- /**
- * The location where all the classes are stored.
- *
- * @var string
- */
- protected static $_rootDir = '';
- /**
- * Variables to say whether you're already setup the autoloader
- *
- * @var boolean
- */
- protected static $_setup = false;
- /**
- * Initializes the auto loader.
- *
- * @param string $rootDir The directory all the classes are located
- */
- public static function setupAutoloader($rootDir)
- {
- if(AutoLoader::$_setup) {
- return;
- }
- AutoLoader::$_rootDir = $rootDir;
- spl_autoload_register('AutoLoader::autoload');
- AutoLoader::$_setup = true;
- }
- /**
- * Will auto load a class by the name of the class.
- *
- * @param string $class The name of the class you're going to be loading.
- *
- * @return boolean
- */
- public static function autoload($class)
- {
- if(class_exists($class)) {
- return true;
- }
- $classFile = AutoLoader::classToFile($class);
- if(!$classFile) {
- return false;
- }
- if(file_exists($classFile)) {
- require_once $classFile;
- return class_exists($class);
- }
- return false;
- }
- /**
- * Converts the class name to a file path.
- *
- * @param string $class The name of the class you want to resolve the file for.
- */
- public static function classToFile($class)
- {
- return AutoLoader::$_rootDir . '/' . strtolower($class) . '.php';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement