Autoloader
Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),
"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),
A detailed description of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in [RFC 2119][].
1. Overview
This PSR is about the specification of automatic loading of corresponding classes by file paths,
This specification is interoperable and can be used as a supplement to any automatic loading specification, including PSR-0, in addition,
This PSR also includes the file storage path specification for automatically loaded classes.
2. Detailed description
The "class" Here refers to all class classes, interfaces, traits reusable blocks of code, and other similar structures.
A complete class name needs to have the following structure:
\<命名空间>(\<子命名空间>)*\<类名>
The complete class name must have a top-level namespace, known as "Vendor namespace";
The full class name can have one or more sub-namespaces;
The complete class name must have a final class name;
The glide line in any part of the complete class name has no special meaning;
The complete class name can consist of any uppercase and lowercase letters;
All class names must be case-sensitive.
When loading the corresponding file according to the full class name ...
In the complete class name, remove the first namespace delimiter, one or more contiguous namespaces and sub-namespaces as "namespace prefixes", which must correspond to at least one "file base directory";
The child namespace immediately following the namespace prefix must match the corresponding file base directory, where the namespace delimiter is used as the directory delimiter.
The class name at the end must have the same name as the corresponding file with the .php
suffix.
The implementation of the Autoloader (autoloader) must not throw an exception, must not trigger any level of error information, and should not have a return value.
3. Example
The following table shows the file paths that correspond to the canonical full class name, namespace prefix, and file base directory.
Full class name |
namespace Prefixes |
File base directory |
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 the implementation of this specification, refer to the relevant examples
Note: Instances are not part of the specification and are subject to change at any time.
The above describes the PHP PSR-4 Autoloader automatic loading Chinese version, including loader aspects of the content, I hope that the PHP tutorial interested in a friend to help.