Permalink
Browse files

Refactored loader

  • Loading branch information...
1 parent e7cac1e commit bfee1f6c6f9eae91910536a26bc263cb0e601165 @shanielh shanielh committed Nov 26, 2011
Showing with 26 additions and 7 deletions.
  1. +26 −7 loader.php
View
@@ -6,15 +6,34 @@
*/
-function class_loader($class_name) {
- include_once 'lib/' . $class_name . '.php';
+class Loader {
+
+ private static $prefixes = [];
+
+ public static function addPrefix($prefix) {
+ array_push(Loader::$prefixes, $prefix);
+ }
+
+ public static function load($class_name) {
+
+ foreach (Loader::$prefixes as $prefix) {
+
+ $suggestedFileName = $prefix . DIRECTORY_SEPARATOR . $class_name . '.php';
+ if (file_exists($suggestedFileName)) {
+ include_once($suggestedFileName);
+ return;
+ }
+ }
+
+ return false;
+
+ }
+
}
-function leander_loader($class_name) {
- include_once 'lib/leander/' . $class_name . '.php';
-}
+Loader::addPrefix('lib');
+Loader::addPrefix('lib/leander');
-spl_autoload_register('class_loader');
-spl_autoload_register('leander_loader');
+spl_autoload_register('Loader::load');
?>

0 comments on commit bfee1f6

Please sign in to comment.