Skip to content

vnaki/loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

YeahPHP类文件加载器 - ClassLoader

YeahPHP类加载器遵守PSR-4PSR-0命名空间规范,具体使用请看下面介绍

  • 首先加载ClassLoader文件
require "yourpath/src/yeahphp/Loader/ClassLoader.php";
  • 创建一个类加载器对象
$classLoader = new ClassLoader();
  • 添加一个PSR-4命名空间规范的前缀
$classLoader->addPsr4($prefix, $directory);

参数解释

参数名称 说明
$prefix 命名空间前缀
$directory 命名空间前缀对应的路径
  • 添加一个PSR-0命名空间规范的前缀
$classLoader->addPsr0($prefix, $directory);

参数解释

参数名称 说明
$prefix 命名空间前缀
$directory 命名空间前缀对应的路径
  • 添加类的映射
$classLoader->addClassMap($class, $file);

参数解释

参数名称 说明
$class 完整的类名称,包含命名空间
$file 类文件路径
  • 通过classmap文件批量添加类的映射
$classLoader->addClassMapFromFile($file);

参数解释

参数名称 说明
$file classmap文件路径

classmap 文件返回结果为一维数组,格式如下

return array(
    "类名称1" => "类文件路径1",
    "类名称2" => "类文件路径2",
    // ... 
);
  • 注册类自动加载器
$classLoader->register($throw, $prepend);
参数名称 说明
$throw 是否允许抛出异常, 默认为 true
$prepend 是否将加载器添加到队列之首, 默认为false
  • 卸载类自动加载器
$classLoader->unregister();

在项目中,你可以直接加载autolaod文件,该文件已经为您初始化了类加载器和一些基础工作

require "yourpath/src/yeahphp/Loader/autoload.php";

About

php class loader, following PSR-4 and PSR-0 specifications

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages