“中国要复兴、富强,必须在开源软件领域起到主导作用,为了国家安全和人类发展,责无旁贷,我们须为此而奋斗”——By:云客
PSR是PHP Standards Recommendations的缩写(php标准推荐),PSR-4规范了如何通过名字空间自动加载类,该规范由php-fig(PHP Framework Interop Group的缩写,php框架交互组)制定,网站http://www.php-fig.org/
以下是规范的中文翻译:
Autoloader
关键词 “必须”("MUST")、“一定不可/一定不能”("MUST NOT")、“需要”("REQUIRED")、
“将会”("SHALL")、“不会”("SHALL NOT")、“应该”("SHOULD")、“不该”("SHOULD NOT")、
“推荐”("RECOMMENDED")、“可以”("MAY")和”可选“("OPTIONAL")的详细描述可参见 [RFC 2119][] 。
1. 概述
本 PSR 是关于由文件路径 自动载入 对应类的相关规范,
本规范是可互操作的,可以作为任一自动载入规范的补充,其中包括 PSR-0,此外,
本 PSR 还包括自动载入的类对应的文件存放路径规范。
2. 详细说明
-
此处的“类”泛指所有的class类、接口、traits可复用代码块以及其它类似结构。
-
一个完整的类名需具有以下结构:
-
完整的类名必须要有一个顶级命名空间,被称为 "vendor namespace";
-
完整的类名可以有一个或多个子命名空间;
-
完整的类名必须有一个最终的类名;
-
完整的类名中任意一部分中的下滑线都是没有特殊含义的;
-
完整的类名可以由任意大小写字母组成;
-
所有类名都必须是大小写敏感的。
-
-
当根据完整的类名载入相应的文件……
-
完整的类名中,去掉最前面的命名空间分隔符,前面连续的一个或多个命名空间和子命名空间,作为“命名空间前缀”,其必须与至少一个“文件基目录”相对应;
-
紧接命名空间前缀后的子命名空间必须与相应的”文件基目录“相匹配,其中的命名空间分隔符将作为目录分隔符。
-
末尾的类名必须与对应的以
.php
为后缀的文件同名。 -
自动加载器(autoloader)的实现一定不能抛出异常、一定不能触发任一级别的错误信息以及不应该有返回值。
-
3. 例子
下表展示了符合规范完整类名、命名空间前缀和文件基目录所对应的文件路径。
完整类名 | 命名空间前缀 | 文件基目录 | 文件路径 |
---|---|---|---|
\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 |
关于本规范的实现,可参阅 相关实例
注意:实例并不属于规范的一部分,且随时会有所变动。
以下是英文原文:
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD","SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to beinterpreted as described inRFC 2119.
1. Overview
This PSR describes a specification for autoloading classes from filepaths. It is fully interoperable, and can be used in addition to any otherautoloading specification, includingPSR-0. This PSR also describes whereto place files that will be autoloaded according to the specification.
2. Specification
-
The term "class" refers to classes, interfaces, traits, and other similarstructures.
-
A fully qualified class name has the following form:
- 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-namespacenames.
- The fully qualified class name MUST have a terminating class name.
- Underscores have no special meaning in any portion of the fullyqualified class name.
- Alphabetic characters in the fully qualified class name MAY be anycombination of lower case and upper case.
- All class names MUST be referenced in a case-sensitive fashion.
-
When loading a file that corresponds to a fully qualified class name ...
- A contiguous series of one or more leading namespace and sub-namespacenames, not including the leading namespace separator, in the fullyqualified 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 thenamespace separators represent directory separators. The subdirectoryname 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.
-
Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errorsof any level, and SHOULD NOT return a value.
3. Examples
The table below shows the corresponding file path for a given fully qualifiedclass name, namespace prefix, and base directory.
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 |
For example implementations of autoloaders conforming to the specification,please see theexamples file. Example implementations MUST NOT be regardedas part of the specification and MAY change at any time.
交流互动