1. Overview
This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can being used in addition to any other autoloading specification, including PSR-0. This PSR also describes, where to place files, that'll be is 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 has a top-level namespace name, also known as a "vendor namespace".
- the fully qualified class name may be one or more sub-namespace names.
- the fully qualified class name must has a terminating class name.
- underscores has no special meaning in any portion of the fully qualified class name.
- alphabetic characters in the fully qualified class name is any combination of lower case and upper case.
The
- all class names must is referenced in a case-sensitive fashion.
When loading a file, 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 Separato R, 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 the throw exceptions, must not raise errors of any, 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 Direc Tory.
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 being regarded as part of the specification and could change at any time.
This specification is an upgrade to the PSR-0 specification,
Here's the difference between PSR-0 and PSR-04,
1) The PSR-04 directory structure is more concise.
2) Note that PSR-0 in the underscore (_) is a special processing, the underscore will be converted to Directory_separator, this is due to the PHP5.3 previous version compatibility considerations, and PSR-4 is not the processing, which is a big difference between the two.
3) PSR-4 requires that the autoloader not be allowed to throw exceptions and raise any level of errors, nor should there be a return value. This is because multiple autoloaders may be registered, and if a autoloader does not find the corresponding class, it should be handed over to the next one instead of blocking the channel.
15th Article PSR-04 Specification