Autoloader
Keywords "MUST", "MUST NOT", "REQUIRED",
"SHALL", " "SHALL NOT"), "should" ("SHOULD"), "should not" ("SHOULD NOT"),
"recommended" ("RECOMMENDED"), "can" ("MAY") and" A detailed description of "OPTIONAL" can be found in [RFC 2119][].
1. Overview
This PSR is a specification related to automatic loading of corresponding classes by file path.
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 corresponding to the automatically loaded class.
2. Detailed description
The "class" here generally refers to all classes, interfaces, traits reusable code blocks and other similar structures.
A complete class name needs to have the following structure:
<code>\<span><<span>命名空间</span>></span>(\<span><<span>子命名空间</span>></span>)*\<span><<span>类名</span>></span></code>
The complete class namemusthave a top-level namespace, called "vendor namespace";
The complete class name Can have one or more sub-namespaces;
The complete class name must have a final class name;
The underscore 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 based on the complete class name...
In the complete class name, remove the first namespace separator, one or more consecutive namespaces and sub-namespaces in front , as a "namespace prefix", it must correspond to at least one "file base directory";
The sub-namespace immediately following the namespace prefix must match the corresponding "file base directory", where The namespace separator will be used as the directory separator. The class name
at the end of must have the same name as the corresponding file with the suffix .php
.
The implementation of the autoloader must not throw an exception, must not trigger any level of error message, and should not have a return value.
3. Example
The following table shows the file paths corresponding to the complete class name, namespace prefix and file base directory that comply with the specification. /ac me-log-writer/lib/
./acme -log-writer/lib/File_Writer.php
AuraWebResponseStatus | AuraWeb | /path/to/aura-web/src/ | /path/to/aura-web/src/Response/Status.php |
---|---|---|---|
SymfonyCore | ./vendor/Symfony/Core/ | ./vendor/Symfony/Core/Request.php | |
Zend | /usr/includes/Zend/ | / usr/includes/Zend/Acl.php | |
For the implementation of this specification, please refer to Related Examples | Note: Examples are not part of the specification and may be changed at any time.The above introduces the PHP PSR-4 Autoloader to automatically load the Chinese version, including the loader content. I hope it will be helpful to friends who are interested in PHP tutorials. |