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
  • 相关阅读:
    Java程序,JDK的安装、环境的配置
    Oracle数据库,序列、索引、视图
    Oracle数据库,内置函数小结
    Oracle数据库,join多表关联方式、union结果集合并
    Oracle数据库,模糊查询、去重查询
    Oracle数据库,查询语句、内置函数
    Oracle数据库,数据的增、删、改、查
    Unity 3D-Canvas画布的三种模式
    Unity 3D-Navigation网格导航系统使用教程
    Xlua使用教程、攻略
  • 原文地址:https://www.cnblogs.com/zhouqingda/p/5175307.html
Copyright © 2011-2022 走看看