Last active
November 2, 2018 10:37
-
-
Save stevenosse/f39f0dac070282189b1fefa27e229f5d to your computer and use it in GitHub Desktop.
Autoloader class that automatically loads all the classes of your project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Author : Steve Nosse | |
* Licence : Freeware | |
* How to use ? | |
* Simply do require_once "autoloader.php"; and then Autoloader::init(); | |
*/ | |
class Autoloader { | |
/** | |
* File extension for the class files | |
* | |
* @var string | |
**/ | |
protected static $extension = '.php'; | |
/** | |
* Directory separator for the file names | |
* | |
* @var string | |
**/ | |
protected static $dir_separator = '/'; | |
/** | |
* Name separator for the namespaces in the file names | |
* | |
* @var string | |
**/ | |
protected static $file_separator = '_'; | |
/** | |
* @return boolean | |
**/ | |
public static function init() { | |
return spl_autoload_register(__CLASS__ . '::load'); | |
} | |
/** | |
* Load the class | |
* | |
* @param string $class The class to load | |
* @return void | |
**/ | |
public static function load($class) { | |
$file = self::getFileName($class); | |
//if (file_exists($file)) { | |
require_once($file); | |
/*} else { | |
throw new Exception('Class ' . $class . ' not exists'); | |
}*/ | |
} | |
/** | |
* Generate the name of the class file | |
* | |
* @param string $class The class name | |
* @return string The file name | |
**/ | |
protected static function getFileName($class) { | |
$file = str_replace( | |
self::$file_separator, | |
self::$dir_separator, | |
$class | |
); | |
return $file . self::$extension; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment