| 15 | | spl_autoload_register('autoload'); |
| 16 | | |
| 17 | | /** |
| 18 | | * A generic autoload function |
| 19 | | * |
| 20 | | * Filename is generated from class name by replacing underscores with |
| 21 | | * directory separators and by adding a '.php' extension. |
| 22 | | * |
| 23 | | * Then the filename is searched from include paths, and if found it is |
| 24 | | * included with require_once(). |
| 25 | | * |
| 26 | | * @param $class string class name to be loaded |
| 27 | | * @return bool true if a class was loaded, false otherwise |
| 28 | | */ |
| 29 | | function autoload($class) |
| 30 | | { |
| 31 | | if (class_exists($class, false)) { |
| 32 | | return false; |
| 33 | | } |
| 34 | | |
| 35 | | $paths = explode(PATH_SEPARATOR, get_include_path()); |
| 36 | | $filename = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; |
| 37 | | |
| 38 | | foreach($paths as $path) { |
| 39 | | if (file_exists($path . DIRECTORY_SEPARATOR . $filename)) { |
| 40 | | require_once($filename); |
| 41 | | return true; |
| 42 | | } |
| 43 | | } |
| 44 | | |
| 45 | | return false; |
| 46 | | } |