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.php
The term "class" refers to classes, interfaces, traits, and other similar structures.git
A fully qualified class name has the following form:github
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
When loading a file that corresponds to a fully qualified class name ...web
.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.spa
The table below shows the corresponding file path for a given fully qualified class name, namespace prefix, and base directory..net
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.code
此規範是對PSR-0規範的升級,orm
下面說說PSR-0和PSR-04的區別,ci
1)PSR-04的目錄結構更加簡潔了。get
2)須要注意PSR-0中對下劃線(_)是有特殊的處理的,下劃線會轉換成DIRECTORY_SEPARATOR,這是出於對PHP5.3之前版本兼容的考慮,而PSR-4中是沒有這個處理的,這也是二者比較大的一個區別。
3)PSR-4要求在autoloader中不容許拋出exceptions以及引起任何級別的errors,也不該該有返回值。這是由於可能註冊了多個autoloaders,若是一個autoloader沒有找到對應的class,應該交給下一個來處理,而不是去阻斷這個通道。