Anfora 自动装载类

时间:2022-10-07 17:05:26

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

 class ClassLoader
{
/* 注册与注销 */ public function loadClass($class)
{
/* 获取文件路径、包含文件,调试信息 */
} public function findFile($class)
{
/* 匹配命名空间规则,返回文件路径 */
}
} function includeFile($file) {
return $include = @include_once $file;
}