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

此规范是对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,应该交给下一个来处理,而不是去阻断这个通道。

最新文章

  1. iOS UINavigationController(内容根据iOS编程编写)
  2. [diango]批量导入不重复数据
  3. ubuntu15.10安装搜狗拼音输入法
  4. 搭建hexo博客
  5. 每天一个linux命令(42):kill命令
  6. jquery中ajax的简单使用
  7. projecteuler Problem 8 Largest product in a series
  8. SQL Server调优系列基础篇 - 并行运算总结(二)
  9. ShadowGun Deadzone 放出 GM Kit Mod 包
  10. 动态规划(水题):COGS 261. [NOI1997] 积木游戏
  11. 微信支付【get_brand_wcpay_request:fail_invalid appid】
  12. [HNOI2012]集合选数
  13. Centos7下安装PHP5.5,5.6,7.0----(转载记录一下)
  14. gittalk报错Error
  15. 通过iptables添加QoS标记
  16. SQL UPDATE 语句
  17. vue-router源码分析
  18. 2.4《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——小结
  19. Angular 4 表单校验2
  20. Linux下onvi支持h265环境的的搭建:gsoap的安装及生产.c .h文件

热门文章

  1. nginx反向代理服务器端口问题
  2. powerdesigner 左边的列表框弄不见了怎么弄出来
  3. web API help pages with Swagger / OpenAPI
  4. The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
  5. HTML5 ——web audio API 音乐可视化(二)
  6. mysql 转移数据目录
  7. XamlParseException异常
  8. 使用Selenium通过浏览器对网站进行自动化测试和相关问题
  9. linux中的kill命令
  10. 原子变量与CAS算法