关于php规范的PSR-4

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. 详细说明

  1. 此处的“类”泛指所有的class类、接口、traits可复用代码块以及其它类似结构。

  2. 一个完整的类名需具有以下结构:

    \<命名空间>(\<子命名空间>)*\<类名>
    
    1. 完整的类名必须要有一个顶级命名空间,被称为 "vendor namespace";

    2. 完整的类名可以有一个或多个子命名空间;

    3. 完整的类名必须有一个最终的类名;

    4. 完整的类名中任意一部分中的下滑线都是没有特殊含义的;

    5. 完整的类名可以由任意大小写字母组成;

    6. 所有类名都必须是大小写敏感的。

  3. 当根据完整的类名载入相应的文件……

    1. 完整的类名中,去掉最前面的命名空间分隔符,前面连续的一个或多个命名空间和子命名空间,作为“命名空间前缀”,其必须与至少一个“文件基目录”相对应;

    2. 紧接命名空间前缀后的子命名空间必须与相应的”文件基目录“相匹配,其中的命名空间分隔符将作为目录分隔符。

    3. 末尾的类名必须与对应的以 .php 为后缀的文件同名。

    4. 自动加载器(autoloader)的实现一定不能抛出异常、一定不能触发任一级别的错误信息以及不应该有返回值。

3. 例子

下表展示了符合规范完整类名、命名空间前缀和文件基目录所对应的文件路径。

完整类名命名空间前缀文件基目录文件路径
\Acme\Log\Writer\File_WriterAcme\Log\Writer./acme-log-writer/lib/./acme-log-writer/lib/File_Writer.php
\Aura\Web\Response\StatusAura\Web/path/to/aura-web/src//path/to/aura-web/src/Response/Status.php
\Symfony\Core\RequestSymfony\Core./vendor/Symfony/Core/./vendor/Symfony/Core/Request.php
\Zend\AclZend/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

  1. The term "class" refers to classes, interfaces, traits, and other similarstructures.

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

    \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
    
    1. The fully qualified class name MUST have a top-level namespace name,also known as a "vendor namespace".
    2. The fully qualified class name MAY have one or more sub-namespacenames.
    3. The fully qualified class name MUST have a terminating class name.
    4. Underscores have no special meaning in any portion of the fullyqualified class name.
    5. Alphabetic characters in the fully qualified class name MAY be anycombination of lower case and upper case.
    6. All class names MUST be referenced in a case-sensitive fashion.
  3. When loading a file that corresponds to a fully qualified class name ...

    1. 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".
    2. 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.
    3. 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 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 NameNamespace PrefixBase DirectoryResulting File Path
\Acme\Log\Writer\File_WriterAcme\Log\Writer./acme-log-writer/lib/./acme-log-writer/lib/File_Writer.php
\Aura\Web\Response\StatusAura\Web/path/to/aura-web/src//path/to/aura-web/src/Response/Status.php
\Symfony\Core\RequestSymfony\Core./vendor/Symfony/Core/./vendor/Symfony/Core/Request.php
\Zend\AclZend/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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值