zoukankan      html  css  js  c++  java
  • 第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.

    2. Specification

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

      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.

    3. Examples

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

    Fully Qualified Class NameNamespace PrefixBase DirectoryResulting File Path
    AcmeLogWriterFile_Writer AcmeLogWriter ./acme-log-writer/lib/ ./acme-log-writer/lib/File_Writer.php
    AuraWebResponseStatus AuraWeb /path/to/aura-web/src/ /path/to/aura-web/src/Response/Status.php
    SymfonyCoreRequest SymfonyCore ./vendor/Symfony/Core/ ./vendor/Symfony/Core/Request.php
    endAcl 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.

    此规范是对PSR-0规范的升级,

    下面说说PSR-0和PSR-04的区别,

    1)PSR-04的目录结构更加简洁了。

    2)需要注意PSR-0中对下划线(_)是有特殊的处理的,下划线会转换成DIRECTORY_SEPARATOR,这是出于对PHP5.3以前版本兼容的考虑,而PSR-4中是没有这个处理的,这也是两者比较大的一个区别。

    3)PSR-4要求在autoloader中不允许抛出exceptions以及引发任何级别的errors,也不应该有返回值。这是因为可能注册了多个autoloaders,如果一个autoloader没有找到对应的class,应该交给下一个来处理,而不是去阻断这个通道。

    本博客的所有博文,大都来自自己的工作实践。希望对大家有用,欢迎大家交流和学习。 我的新站:www.huishougo.com
  • 相关阅读:
    ugui点击穿透判断
    c#字符串代码,动态创建编译器
    github项目分享
    unity 2d 版的lookAt
    unity全屏截图
    shader例子
    AcWing 329. 围栏障碍训练场
    AcWing 326. XOR和路径
    AcWing 324. 贿赂FIPA
    AcWing 322. 消木块
  • 原文地址:https://www.cnblogs.com/zhouqingda/p/5175307.html
Copyright © 2011-2022 走看看