The translation and view of the automatic loading norm of PSR class

Source: Internet
Author: User
Tags autoloader deprecated

Some resources are listed first:

    • PSR-0 website Original
    • GitHub Source for PSR
    • The GitHub source of the PSR Chinese translation

There are several points to note beforehand:

    • In the translation section, I only select the main specifications in PSR-0 and PSR-4 for translation.
    • My translation focuses on understanding the angle rather than the strict grammatical translation.

Description of key modifiers:

**MUST**        __务必__**MUST_NOT**    __绝不__**REQUIRED**    __务必__**SHALL**       __务必__**SHALL_NOT**   __绝不__**SHOULD**      __应该__**SHOULD_NOT**  __不应该__**RECOMMENDED** __建议__**MAY**         __可以__**OPTIONAL**    __可选__

Why, look at the definition of rfc2119!

PSR-0 class automatic loading specification (autoloading standard)

deprecated , the PSR-0 specification has been deprecated since October 21, 2014 and is recommended as an alternative to the PSR-4 specification.

Specification Body
    1. Namespaces (Namespace) and Classes (Class) must have a standard structure as shown on the right \<Vendor Name>\(<Namespace>\)*<Class Name> .
    2. (All) namespaces (Namespace) must have a top-level namespace, such as: Vendor Name .
    3. (All) namespaces (Namespace) You can choose to define multiple child namespaces (note not required), depending on your actual needs.
    4. At load time, the (All) namespace (Namespace) delimiter is \ converted to a directory separator DIRECTORY_SEPARATOR .
    5. The class name is _ converted to a directory separator DIRECTORY_SEPARATOR , but the namespace (Namespace) _ does not have any meaning (purely cosmetic).
    6. When loading, namespaces (Namespace) and classes (class), the source file should be suffixed with. PHP (this rule is actually defined in general).
    7. When naming, the namespace (Vendor name,namespace), class name, should be composed of half-width letters, uppercase and lowercase.
Demonstrate
return [    ‘\Doctrine\Common\IsolatedClassLoader‘ => ‘/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php‘,    ‘\Symfony\Core\Request‘ => ‘/path/to/project/lib/vendor/Symfony/Core/Request.php‘,    ‘\Zend\Acl‘ => ‘/path/to/project/lib/vendor/Zend/Acl.php‘,    ‘\Zend\Mail\Message‘ => ‘/path/to/project/lib/vendor/Zend/Mail/Message.php‘,];
Underlining the use of namespaces and class names
return [    ‘\namespace\package\Class_Name‘ => ‘/path/to/project/lib/vendor/namespace/package/Class/Name.php‘,    ‘\namespace\package_name\Class_Name‘ => ‘/path/to/project/lib/vendor/namespace/package_name/Class/Name.php‘];
PSR-4 class loader (Autoloader)
  1. The classes (Class) in this article can refer to classes (class, which can be understood here as code-defined structures for classes), interfaces (Interface), features (Trait), and so on.
  2. The fully qualified class name, as shown on the right\<顶级命名空间>(\<子级命名空间>)*\<类名>
    1. The class name must have a top-level namespace, commonly known as "Vendor namespace".
    2. A class name can have one or more sub-namespace names.
    3. The name of the class must be terminated with a class name (ClassName).
    4. Underscores _ no longer have any justification (as distinct from psr-0#5).
    5. The English alphabet in the class name can be any combination of the uppercase and lowercase.
    6. All class names are referenced, so be sure to follow their named case style.
  3. Consistency rules when loading (a class source file):
    1. Prefix mapping rules: The namespace prefix in the class name, minus the leftmost delimiter, is \ mapped to an underlying directory (note the namespace prefix, which can refer to a single namespace prefix, or multiple namespace prefixes, such as acme\ Log points to a directory, and then point Acme\log\writer to another directory).
    2. The prefix maps the remaining sub-namespaces, so be sure to correspond to the directory structure one by one under the underlying directory that he maps (this only says directory structure, not file naming).
    3. The class source file needs to be .php terminated and must match the class naming case.
  4. Class loader (Autoloader), never throws any exceptions, any level of errors, and returns arbitrary results.
View

PSR-0 is actually a completely non-binding norm, since the 3rd has completely failed to give a strong modifier.

PSR-4 is more pragmatic, from standard to ER. But this prefix mapping method, from AGIMVC 3 I began to play, but to be honest, this thing is a chicken. Why do you say that?

Prefix mapping in PSR-4, is infinite mapping, assuming I have a class full name \My\Sub1\Sub2\Sub3\ClassA , I will My\Sub1\Sub2 point to the directory 1, will My\Sub1 point to the directory 2, then when referencing this class, loadclass should be how to design it? The most straightforward approach is to match from the My\Sub1\Sub2\Sub3 beginning, then My\Sub1\Sub2 I hit the directory 1. God, if I have a lot of namespaces in my definition, how many loops does this match take? So even composer has to provide an optimized parameter to enumerate all the defined classes of a class library.

At the beginning of the project, we may really need an unlimited amount of space, but into the specific development process, everything is concrete can not be more specific.

Or, this is the PHP language itself is the failure of the place, he unswervingly walk on the path of the Microsoft Unlimited copy, but do not give any specific language specifications, or even a more advanced loading mechanism, and all of them to his behind the many Cottage Framework Alliance--PSR Alliance.

Composer and PSR put all the stupid pit-daddy performance stuff in a brain-drain on PHP, the worst-performing development language ...

A few days ago with node. js to do something, really feel that node. JS's package mechanism is clear and specific. PHP can not learn to learn node. js, Ah, was later the younger brother beyond, PHP ah, you should also shake your old baggage?

The translation and view of the automatic loading norm of PSR class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.