接上回的继续说,上回说到PSR-3日志记录器接口,这回我们来说说PSR的最后一个标准,PSR-4,自动加载器策略。

缘由

自动加载器策略是指,在运行时按需查找PHP类、接口或性状,并将其载入PHP解释器。

支持PSR-4自动加载器标准的PHP组件和框架,使用同一个自动加载器就能找到相关代码,然后将其载入PHP解释器。

怎么来理解呢,你可能看PHP代码的时候经常会看到下面的代码

<?php
include 'path/to/file1.php';
include 'path/to/file2.php';
include 'path/to/file3.php';

如果需要引入一百个,一千个PHP脚本,include()函数就不能胜任了。有了自动加载器,我们就无需这样引入文件,自动加载器策略能找到PHP类、接口或性状,然后在运行时按需将其载入PHP解释器。

大多数现代的PHP组件和框架都符合PSR-4规范,如果你要自己编写并分发PHP组件,确保你的组件也符合PSR-4规范。

PSR-4

1、The term “class” refers to classes, interfaces, traits, and other similar structures.

2、A fully qualified class name has the following form:

 \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

The fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”.

The fully qualified class name MAY have one or more sub-namespace names.

The fully qualified class name MUST have a terminating class name.

Underscores have no special meaning in any portion of the fully qualified class name.

Alphabetic(按字母排序的) characters in the fully qualified class name MAY be any combination of lower case and upper case.

All class names MUST be referenced in a case-sensitive fashion.

3、When loading a file that corresponds to a fully qualified class name …(加载完整路径类名)

A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a “namespace prefix”) corresponds to at least one “base directory”.

The contiguous sub-namespace names after the “namespace prefix” correspond to a subdirectory within a “base directory”, in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

4、Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.

例子

    FULLY QUALIFIED CLASS NAME     NAMESPACE PREFIX     BASE DIRECTORY     RESULTING FILE PATH
    \Acme\Log\Writer\File_Writer     Acme\Log\Writer     ./acme-log-writer/lib/     ./acme-log-writer/lib/File_Writer.php
    \Aura\Web\Response\Status     Aura\Web     /path/to/aura-web/src/     /path/to/aura-web/src/Response/Status.php
    \Symfony\Core\Request     Symfony\Core     ./vendor/Symfony/Core/     ./vendor/Symfony/Core/Request.php
    \Zend\Acl     Zend     /usr/includes/Zend/     /usr/includes/Zend/Acl.php

PSR-4的精髓是把命名空间的前缀和文件系统中的目录对应起来。例如,我可以告诉PHP,\Feng\YePHP命名空间中的类、接口和性状在物理文件系统的src/目录中,这样PHP就知道,前缀为\Feng\YePHP的命名空间中的类、接口和性状对应于src/目录里的目录和文件。

如何编写

<?php
/**
* An example of a project-specific implementation.
*
* After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
* from /path/to/project/src/Baz/Qux.php:
*
* new \Foo\Bar\Baz\Qux;
*
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class) { // project-specific namespace prefix
$prefix = 'Foo\\Bar\\'; // base directory for the namespace prefix
$base_dir = __DIR__ . '/src/'; // does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
} // get the relative class name
$relative_class = substr($class, $len); // replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; // if the file exists, require it
if (file_exists($file)) {
require $file;
}
});

复制以上代码,将其粘贴到你的应用中,然后修改变量$prefix和$base_dir的值,这样你就可以拥有一个可用的PSR-4自动加载器了。

但是你大可不必这么做,因为我们可以使用依赖管理器Composer自动生成的PSR-4自动加载器。我们会在后续的博文中提及这个Composer。

最新文章

  1. POJ 2352
  2. 系统巡警 v1.2 系统行为分析神器
  3. mysql,sql server,oracle 唯一索引字段是否允许出现多个 null 值?
  4. java 小知识点
  5. freeswitch
  6. JDBC资料集
  7. Android 批量插入数据到SQLite数据库
  8. 读《HTML5与CSS3权威指南(上册)》笔记
  9. Python中的isinstance函数
  10. .Net程序员学用Oracle系列(7):视图、函数、过程、包
  11. Eclipse 多行复制并且移动失效
  12. Django实现注册页面_头像上传
  13. CentOS6.5安装pip
  14. Intellij IDEA 设置启动JVM参数
  15. vue2.0 之标签属性
  16. android greenDao使用
  17. Markdown语法学习(二)
  18. OpenGL 用三角形模拟生成球面
  19. (原创)OpenStack服务如何使用Keystone (二)---部署和配置Keystone中间件
  20. python面向对象高级:定制类

热门文章

  1. sqlserver 设置外键
  2. Android 使用AsyncTask 下载图片的例子,学会使用AsyncTask
  3. zepto js 源码 解读
  4. PHP新手之学习数组声明
  5. 网上搜集的一段php可逆加密函数
  6. Yii 1.0 基础
  7. js架构设计模式——前端MVVM框架设计及实现(一)
  8. 查看Eclipse版本号,及各个版本区别
  9. 删除坐标相同文本对象 按照原y坐标排序
  10. Fiddler 模拟请求的操作方法