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

第15篇 PSR-04 规范的更多相关文章

  1. Vue 2.5 发布了:15篇前端热文回看

    Vue 2.5 发布了:15篇前端热文回看 2017-11-02 前端大全 (点击上方公众号,可快速关注) 本文精选了「前端大全」2017 年 10 月的 15 篇热门文章.其中有职场分享.技术分享和 ...

  2. PHP PSR 代码规范基本介绍

    PSR 是 PHP Standard Recommendation 的简写,即PHP推荐标准. 目前通过的规范有 PSR-0(Autoloading Standard).PSR-1(Basic Cod ...

  3. Python和Flask真强大:不能错过的15篇技术热文(转载)

    Python和Flask真强大:不能错过的15篇技术热文 本文精选了 Python开发者 11月份的15篇 Python 热文.其中有基础知识,机器学习,爬虫项目实战等. 注:以下文章,点击标题即可阅 ...

  4. STM32系列第15篇--灵活的静态存储控制器FSMC

    源: STM32系列第15篇--灵活的静态存储控制器FSMC

  5. 6 大主流 Web 框架优缺点对比:15篇前端热文回看

    摘自:http://blog.csdn.net/VhWfR2u02Q/article/details/78993079 注:以下文章,点击标题即可阅读 <6 大主流 Web 框架优缺点对比> ...

  6. PHP PSR标准规范

    PHP PSR标准规范,PHP开发者都需要遵循规范. 官网(英文版本): https://www.php-fig.org 官网(中文版本): https://psr.phphub.org

  7. PSR标准规范

    PSR标准规范 基本代码规范 PHP代码文件 必须 以 不带 BOM 的 UTF-8 编码: 类的命名 必须 遵循 StudlyCaps 大写开头的驼峰命名规范: 类中的常量所有字母都 必须 大写,单 ...

  8. psr的规范

    基本代码规范 本篇规范制定了代码基本元素的相关标准, 以确保共享的PHP代码间具有较高程度的技术互通性. 关键词 "必须"("MUST")."一定不可 ...

  9. PHP PSR基本代码规范(中文版)

    PSR-1 基本代码规范 本篇规范制定了代码基本元素的相关标准,以确保共享的PHP代码间具有较高程度的技术互通性. 关键词 “必须”("MUST").“一定不可/一定不能”(&qu ...

随机推荐

  1. java object 转为 json

    JSONObject jsonObject=JSONObject.fromObject(map) 执行到这的时候没有任何反应的原因及解决办法 http://blog.csdn.net/tjcyjd/a ...

  2. redis的Python接口调用

    Redis安装及教程: redis教程 安装Python的redis接口模块 redis-py requires a running Redis server. See redis教程 for ins ...

  3. JDK_如何查看安装的jdk是32位还是64位?

    1. 1.1.32位系统只能装 32位 jdk 1.2.64位系统,安装的 32位JDK 和 64位JDK 是不同的目录 1.2.1.32位的路径 类似:C:\Program Files (x86)\ ...

  4. JAR_给别人使用

    1. http://zhidao.baidu.com/link?url=Ru2tGNV5iRsuRYlEdWzmKDRz88aYqHAqQBQy8sCaHWhjJpaJpbTGibEk-zyxegNJ ...

  5. java手动分页处理

    经常我们在操作数据库的时候都会用到分页,而且很多框架也提供了分页功能,像PageHelper. 但是在有些项目中,需要将数据查询出来进行手动分页,那么原理是什么呢? 其实很简单,首先需要知道数据总量, ...

  6. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  7. python之list,tuple,str,dic简单记录(一)

    list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目.列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表.一旦你创建了一个列表,你就可以添加 ...

  8. 解决:夜神模拟器连不上adb的问题

    一. adb devices发现不了设备 刚试了一下,在夜神模拟器开启的情况下,adb devices 死活找不到设备 adb kill-server和adb start-server也无济于事 二. ...

  9. java开发工具idea,在install时候报错The packaging for this project did not assign a file to the build artifact

    intellij中install报错:The packaging for this project did not assign a file to the build artifact 原因是run ...

  10. jdbc之防sql注入攻击

    1.SQL注入攻击:    由于dao中执行的SQL语句是拼接出来的,其中有一部分内容是由用户从客户端传入,所以当用户传入的数据中包含sql关键字时,就有可能通过这些关键字改变sql语句的语义,从而执 ...