What are the specifications of the PSR series in PHP?

Source: Internet
Author: User
Tags php framework php tutorial
Psr

PHP Standard recommendation shorthand, it should be called PSRs, that is, the series of recommended standards: the current adoption of the specification has PSR-0 (autoloading), PSR-1 (Basic Coding) , PSR-2 (Coding Style Guide), PSR-3 (Logger Interface), PSR-4 (improved autoloading). It is not the official PHP standard, but a series of standards extracted from well-known PHP projects such as Zend, Symfony2, and now more and more community projects are joining members and following that standard.

php-fig-php Framework Interoperability Group is an organization that formulates PSRS development specifications, and its members are made up of many well-known PHP community projects, and a list of members can be found at the bottom of their homepage. These members develop specifications and are implemented in their own projects.

In fact, the content of each PSR specification is very concise and clear, compared to those who move the dozens of-page specification, is really good too much.

PSR-0 (autoloading Standard)

PSR-0-Class Automatic Loading specification (original: official website, GitHub). From 2014-10-21 onwards, the specification was marked as deprecated and replaced by PSR-4. Its contents are very concise.

Content:

    1. A fully qualified namespace and class name must have the following structure "\< Vendor Name >\ (< Namespace >\) *< Class Name >"
    2. Each namespace must have a top-level namespace ("Vendor name")
    3. Each namespace can have any number of sub-namespaces
    4. Each namespace must be converted to the operating system path delimiter (Directory_separator) when it is loaded from the file system
    5. Each "_" character is converted to Directory_separator in the class name. The "_" symbol has no explicit meaning in the namespace
    6. Namespaces and class names that conform to the naming standard must be loaded with the end of ". PHP"
    7. Vendor Name, a namespace in which the class name can consist of uppercase and lowercase letters, where the namespace and class names are case sensitive to ensure multiple system compatibility

PSR-1 (Basic Coding Standard)

PSR-1 is the basic coding standard (original: website, GitHub). The content is also concise, including the class file, class name, class method name naming methods.

Content:

    1. The source file must use only

    2. The encoding format of the PHP code in the source file must only be UTF-8 with no BOM

    3. A source file is recommended only for declarations (classes, functions, constants, etc.) or only for actions that cause side effects (for example, output information, modify. ini configuration files, etc.), but should not do both things at the same time

    4. Namespaces and classes must comply with the PSR-0 standard

    5. Class names must be written using StudlyCaps

    6. Constants in a class must consist only of uppercase letters and underscores (_)

    7. Method names must be written using the camecase notation

PSR-2 (Coding Style Guide)

PSR-2 is the coding style standard (original: official website, GitHub). Slightly more content, but still relatively concise, it is based on PSR-1, including indentation, each line of code length, line wrapping, method visibility declarations, spaces, and method body braces for wrapping the relevant provisions.

Standard content:

    1. Code must follow PSR-1

    2. The code must use 4 spaces to indent instead of a tab

    3. The length of a line of code should not have a hard limit, the soft limit must be 120 characters, it is recommended that each line of code 80 characters or less

    4. There must be a blank line under the Declaration of the namespace, and there must be a line below the declaration of the Use

    5. The left curly brace of a class must be placed in a row below its declaration, and the right curly brace must be placed in a row below the body of the class

    6. The left curly brace of a method must be placed in a row below its declaration, and the right curly brace must be placed on the next line of the method body

    7. All properties and methods must have a visibility declaration; the abstract and final declarations must precede the visibility declaration, and the static declaration must be preceded by a declaration of visibility

    8. There must be a space after the structure control keyword, and no spaces after methods and function calls

    9. The left curly brace of the structure control must be placed on the same line as the right curly brace must be placed on the next line of the structure control code body

    10. No spaces after the left parenthesis of the control structure, no spaces before the right parenthesis

PSR-3 (Logger Interface)

PSR-3 is the definition of the pass-through interface to the Application log Class (original: Official website, GitHub). The content is simple, is an interface, the official sample code to reference it. Of course, in a specific application, as long as you follow this interface, you can certainly customize the corresponding implementation.

Basic content:

    1. Loggerinterface exposes eight interfaces for logging eight levels (debug, info, notice, warning, error, critical, alert, emergency).

    2. The Nineth method is log, which accepts the log level as the first parameter. Calling this method with a log rank constant must be consistent with the result of invoking the specified hierarchy method directly. Calling the method with a log level that is not defined in this specification and is not known for a specific implementation must throw a psr\log\invalidargumentexception. It is not recommended to use a custom log level unless you are very sure that the current class library has support for it.

PSR-4 (improved autoloading)

PSR-4 is an improved version of the automatic Loading specification (original: official website, GitHub). It is the successor to the PSR-0 specification. It can be compatible with any other auto-loading specification, including PSR-0.

Content:

    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: ( ) *
      1. A fully qualified class name must have a top-level namespace (Vendor name)

      2. Fully qualified class names can have many child namespaces

      3. A fully qualified class name should have a terminating class name

      4. Underscores do not have a 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 case-sensitive to reference

    3. When you load a file from a fully qualified class name:
      1. In a fully qualified class name, a contiguous namespace prefix consisting of one or more sub-namespaces (excluding the delimiter of the top-level namespace), 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 a file that ends in. php. The file name must match the terminating class name case

    4. The implementation of the automatic loader cannot throw any exceptions, cannot raise any level of error, and should not return a value

Note: The specific specifications refer to other Chinese translation version collation.

The above describes what the PSR series of PHP specifications have, including the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.