Anfora 自动装载类

1、目录结构

+ Anfora

   | + Autoload

        | - ClassLoader.php

   | - Autoload.php

- autoload.php

2、注册类加载器

src/Anfora/Autoload.php

class Anfora_Autoload
{
    private static $loader;
    
    public static function loadClassLoader($class)
    {
        $class_name = trim($class, '\\');
        if ('Anfora\Autoload\ClassLoader' === $class_name) {
            require_once __DIR__ . '/Autoload/ClassLoader.php';
        }
    }
    
    public static function getLoader()
    {
        if (null !== self::$loader) {
            return self::$loader;
        }
        
        $autoload_function = array('Anfora_Autoload', 'loadClassLoader');
        spl_autoload_register($autoload_function, true, true);
        self::$loader = $loader = new \Anfora\Autoload\ClassLoader();
        spl_autoload_unregister($autoload_function);
        $loader->register(true);
        return $loader;
    }
}

getLoader()

注册类加载器函数并注销

注册全局类加载

loadClassLoader()

引入全局类加载

3、全局类装载

src/Anfora/Autoload/ClassLoader.php

 1 class ClassLoader
 2 {
 3     /* 注册与注销 */
 4 
 5     public function loadClass($class)
 6     {
 7         /* 获取文件路径、包含文件,调试信息 */
 8     }
 9 
10     public function findFile($class)
11     {
12         /* 匹配命名空间规则,返回文件路径 */
13     }
14 }
15 
16 
17 function includeFile($file) {
18     return $include = @include_once $file;
19 }
原文地址:https://www.cnblogs.com/wudi/p/8508805.html