天天看點

關于php規範的PSR-4Autoloader

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_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

  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 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.