PHP specification PSR0 and PSR4 understanding

Source: Internet
Author: User
Tags autoload vars

I. Introduction of PSR0

The following describes the specifications that you need to follow to use a generic 自动加载器(autoloader) :

    • A completely standard 命名空间(namespace) and 类(class) structured structure is this:\<Vendor Name>\(<Namespace>\)*<Class Name>
    • Each 命名空间(namespace) must have a top-level 空间名(namespace) (" 组织名(Vendor Name) ").
    • 命名空间(namespace)you can use as many as you want in each 子命名空间(sub-namespace) .
    • When the source file is loaded from the file system, 空间名(namespace) the delimiter in is converted to DIRECTORY_SEPARATOR .
    • 类名(class name)Each underline in the _ will be converted to one DIRECTORY_SEPARATOR . The underline _ 空间名(namespace) has no special meaning in the.
    • A suffix is 命名空间(namespace) added when the source file is fully standard and 类(class) loaded from the file system .php .
    • 组织名(vendor name), 空间名(namespace) which 类名(class name) are made up of uppercase and lowercase letters.
Reference: http://www.php-fig.org/psr/psr-0/below, lists several forms of the canonical class that PSR0 constructs:

Second, realize PSR0 automatic loading

[PHP]View PlainCopy
  1. function AutoLoad ($className)
  2. {
  3. $className = LTrim ($className, ' \ \ ');
  4. $fileName = ";
  5. $namespace = ";
  6. if ($lastNsPos = Strrpos ($className, ' \ \ ')) {
  7. $namespace = substr ($className, 0, $lastNsPos);
  8. $className = substr ($className, $lastNsPos + 1);
  9. $fileName = str_replace (' \ \ ', Directory_separator, $namespace).  Directory_separator;
  10. }
  11. $fileName. = Str_replace (' _ ', Directory_separator, $className).  '. php ';
  12. require $fileName;
  13. }
  14. Spl_autoload_register (' autoload ');

Iii. Introduction to PSR4 This PSR describes the guidelines for automatically loading classes through file paths; it serves as a supplement to PSR-0, and is automatically loaded according to the instructions on how to standardize the storage files;
    1. The term "class" is a general term; it contains classes, interfaces, traits, and other similar structures;

    2. The fully qualified class name should resemble the following example:

      <NamespaceName> (<SubNamespaceNames>) *<classname>

      1. The fully qualified class name must have a top-level namespace (Vendor name);
      2. A fully qualified class name can have many child namespaces;
      3. A fully qualified class name should have a terminating class name;
      4. The underscore has no special meaning in the fully qualified class name;
      5. Letters can be any combination of uppercase and lowercase in a fully qualified class name;
      6. All class names must be referred to in case-sensitive manner;
    3. When you load a file from a fully qualified class name:

      1. In a fully qualified class name, a contiguous namespace prefix (excluding the delimiter of the top-level namespace) that consists of one or more sub-namespaces, at least one underlying directory.
      2. The contiguous sub-namespace name after the namespace prefix corresponds to a subdirectory under the base directory, where the namespace delimiter represents the directory delimiter. The subdirectory name must match the child namespace name case;
      3. The terminating class name corresponds to an .php end file. The file name must match the case of the terminating class name;
    4. The implementation of the automatic loader cannot throw any exception, cannot raise any level of error, and should not return a value;

Reference: http://www.php-fig.org/psr/psr-4/in which there are the following class instances, but relative to PSR0, it is not easy to understand first, \symfony\core\request and \zend\acl very well understood, It satisfies the PSR0 specification, but \acme\log\writer\file_writer is not well understood. Four, composer to PSR4 treatment see Composer to PSR4 treatment, can compare easy to understand PSR4. The automatically generated PSR4 profile name is called autoload_psr4.php (PSR0 is autoload_namespace.php), the configuration file returns an associative array, the key is the prefix of the namespace, and the value is the path to the namespace prefix. As an example of \acme\log\writer\file_writer, whose namespace prefix is \acme\log\writer, it is represented in autoload_psr4.php as
    1. ' \\Acme\\Log\\Writer ' (array) ' ./acme-log-writer/lib/'
When using automatic loading, the corresponding mapping path is found by the prefix of the namespace, and the corresponding class definition file is found according to the PSR0 specification. There are, however, exceptions where the underscore in the class name does not need to be converted to a directory in PSR4. Using PSR4, I think there are 2 benefits: 1. Reduce the code catalog depth by 2. Can quickly find the mapping directory by prefix, improve the efficiency of automatic loading

PHP specification PSR0 and PSR4 understanding

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.