Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file autoload.php
- * @author <[email protected]>
- * @date Sat Jul 21 13:37:50 2012
- * @see http://swwwfactory.blogspot.com/
- *
- * @brief Simple autoload your classes
- *
- *
- */
- namespace com\my\stuff;
- class Autoload
- {
- static protected $db = [
- //add here my classes
- 'com\my\stuff\Example' => 'path/to/example.php',
- 'com\my\another\stuff\Example' => 'path/to/another.foo.bar.example.php'
- ];
- public static function init()
- {
- return spl_autoload_register(__NAMESPACE__ .'\Autoload::handler');
- }
- public static function handler($name)
- {
- $db = self::$db;
- if (isset($db[$name])) {
- $module = __DIR__ . '/' . $db[$name];
- //uncomment this to debug
- //echo 'try load:', $module, ' for class:', $name, '<br>';
- //echo '<pre>', debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 0),
- // '</pre>';
- //$old = get_declared_classes();
- include $module;
- //uncomment this to debug
- //echo '<pre>',
- // print_r(array_diff(get_declared_classes(), $old), true),
- // '</pre>';
- //echo 'done load.', '<br><br>';
- return true;
- }
- return false;
- }
- public static function testClasses()
- {
- array_walk(
- self::$db,
- function ($elem, $key) {
- return class_exists($key);
- }
- );
- }
- public static function getLoadMap()
- {
- return self::$db;
- }
- }
- Autoload::init();
- //uncomment this for autoload tests
- //Autoload::testClasses();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement