第15篇 PSR-04 規範

1. Overview

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.php

2. Specification

  1. The term "class" refers to classes, interfaces, traits, and other similar structures.git

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

    \<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-namespace names.
    3. The fully qualified class name MUST have a terminating class name.
    4. Underscores have no special meaning in any portion of the fully qualified class name.
    5. Alphabetic characters in the fully qualified class name MAY be any combination 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 ...web

    1. 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".
    2. 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.
    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 errors of any level, and SHOULD NOT return a value.spa

3. Examples

The table below shows the corresponding file path for a given fully qualified class name, namespace prefix, and base directory..net

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 the examples file. Example implementations MUST NOT be regarded as part of the specification and MAY change at any time.code

 

此規範是對PSR-0規範的升級,orm

下面說說PSR-0和PSR-04的區別,ci

1)PSR-04的目錄結構更加簡潔了。get

2)須要注意PSR-0中對下劃線(_)是有特殊的處理的,下劃線會轉換成DIRECTORY_SEPARATOR,這是出於對PHP5.3之前版本兼容的考慮,而PSR-4中是沒有這個處理的,這也是二者比較大的一個區別。

3)PSR-4要求在autoloader中不容許拋出exceptions以及引起任何級別的errors,也不該該有返回值。這是由於可能註冊了多個autoloaders,若是一個autoloader沒有找到對應的class,應該交給下一個來處理,而不是去阻斷這個通道。

相關文章
相關標籤/搜索