第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
The term "class" refers to classes, interfaces, traits, and other similar structures.
A fully qualified class name has the following form:
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
- The fully qualified class name MUST have a top-level namespace name, also known as a "vendor namespace".
- The fully qualified class name MAY have one or more sub-namespace names.
- The fully qualified class name MUST have a terminating class name.
- Underscores have no special meaning in any portion of the fully qualified class name.
- Alphabetic characters in the fully qualified class name MAY be any combination of lower case and upper case.
- All class names MUST be referenced in a case-sensitive fashion.
When loading a file that corresponds to a fully qualified class name ...
- 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".
- 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.
- The terminating class name corresponds to a file name ending in
.php
. The file name MUST match the case of the terminating class name.
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 规范的更多相关文章
- Vue 2.5 发布了:15篇前端热文回看
Vue 2.5 发布了:15篇前端热文回看 2017-11-02 前端大全 (点击上方公众号,可快速关注) 本文精选了「前端大全」2017 年 10 月的 15 篇热门文章.其中有职场分享.技术分享和 ...
- PHP PSR 代码规范基本介绍
PSR 是 PHP Standard Recommendation 的简写,即PHP推荐标准. 目前通过的规范有 PSR-0(Autoloading Standard).PSR-1(Basic Cod ...
- Python和Flask真强大:不能错过的15篇技术热文(转载)
Python和Flask真强大:不能错过的15篇技术热文 本文精选了 Python开发者 11月份的15篇 Python 热文.其中有基础知识,机器学习,爬虫项目实战等. 注:以下文章,点击标题即可阅 ...
- STM32系列第15篇--灵活的静态存储控制器FSMC
源: STM32系列第15篇--灵活的静态存储控制器FSMC
- 6 大主流 Web 框架优缺点对比:15篇前端热文回看
摘自:http://blog.csdn.net/VhWfR2u02Q/article/details/78993079 注:以下文章,点击标题即可阅读 <6 大主流 Web 框架优缺点对比> ...
- PHP PSR标准规范
PHP PSR标准规范,PHP开发者都需要遵循规范. 官网(英文版本): https://www.php-fig.org 官网(中文版本): https://psr.phphub.org
- PSR标准规范
PSR标准规范 基本代码规范 PHP代码文件 必须 以 不带 BOM 的 UTF-8 编码: 类的命名 必须 遵循 StudlyCaps 大写开头的驼峰命名规范: 类中的常量所有字母都 必须 大写,单 ...
- psr的规范
基本代码规范 本篇规范制定了代码基本元素的相关标准, 以确保共享的PHP代码间具有较高程度的技术互通性. 关键词 "必须"("MUST")."一定不可 ...
- PHP PSR基本代码规范(中文版)
PSR-1 基本代码规范 本篇规范制定了代码基本元素的相关标准,以确保共享的PHP代码间具有较高程度的技术互通性. 关键词 “必须”("MUST").“一定不可/一定不能”(&qu ...
随机推荐
- Spring注解(事务)
spring操作数据库 jdbc <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> & ...
- [POI2006]MET-Subway
Description 给出一棵N个结点的树,选择L条路径,覆盖这些路径上的结点,使得被覆盖到的结点数最多. Input 第一行两个正整数N.L(2 <= N <= 1,000,000, ...
- 查询当天数据(mysql)
SELECT count(*) as nums FROM go_member_share WHERE DATEDIFF(FROM_UNIXTIME(time, '%Y-%m-%d') , now()) ...
- PHP中的定界符
因为PHP是一个Web编程语言,在编程过程中难免会遇到用echo来输出大段的html和javascript脚本的情况,如果用传统的输出方法——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等 ...
- 【spark】常用转换操作:sortByKey()和sortBy()
1.sortByKey() 功能: 返回一个根据键排序的RDD 示例 val list = List(("a",3),("b",2),("c" ...
- HTML——部分MP4在谷歌浏览器上无法播放
Chrome浏览器支持HTML5,它支持原生播放部分的MP4格式(不用通过Flash等插件). 为什么是部分MP4呢?MP4有非常复杂的含义(见http://en.wikipedia.org/wiki ...
- ActionDescriptor 的认识
ActionDescriptor的作用是对Action方法的元数据的描述,通过ActionDescriptor我们可以获取到action方法的相关的名称,所属控制器,方法的参数列表,应用到方法上的特性 ...
- scorm标准的LMS在客户端的运行机制
1)运行SCORM APIAdapter. 2)调用API初始化函数. 3)加载课件SCO初始化数据. 4)获取Data Model中的用户ID和用户姓名. 5)获取Data Mode ...
- 《Drools7.0.0.Final规则引擎教程》第4章 4.2 no-loop
no-loop 定义当前的规则是否不允许多次循环执行,默认是 false,也就是当前的规则只要满足条件,可以无限次执行.什么情况下会出现规则被多次重复执行呢?下面看一个实例: package com. ...
- Intellij IDEA带参数启动Springboot注意事项
问题 不同版本的spring-boot-maven-plugin的jvm参数配置有所不同,同时与通过main方法启动springboot程序传递参数也有所不同. 分析 在运行main方法时,可以通过j ...