What are the content of PHP's series of SRS?

Source: Internet
Author: User
: This article mainly introduces the content of the series of PHP-related SRs. if you are interested in the PHP Tutorial, please refer to it. PSR

Is short for PHP Standard Recommendation, it should actually be called PSRs, that is, the series of Recommendation standards: currently through the specification of PSR-0 (Autoloading Standard), PSR-1 (Basic Coding Standard), PSR-2 (Coding Style Guide), PSR-3 (Logger Interface), PSR-4 (Improved Autoloading ). It is not an official PHP standard, but a series of standards extracted from well-known PHP projects such as Zend and Symfony2. Currently, more community projects are joining members and following this standard.

PHP-FIG-PHP Framework Interoperability Group is an organization that develops PSRs development specifications. its members are composed of many well-known PHP community projects and can be seen at the bottom of its home page. These members develop specifications and implement them in their own projects.

In fact, the content of various DSRs specifications is concise and clear, which is much better than the dozens of pages of those specifications.

PSR-0 (Autoloading Standard)

PSR-0 is class automatic loading specification (original: Official Site, GitHub ). From, the specification was marked as Deprecated and replaced by a PSR-4. Its content is 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 separator" (DIRECTORY_SEPARATOR) when being loaded from the file system)
  5. Each "_" character is converted to DIRECTORY_SEPARATOR in the "class name. The '_' symbol does not have a clear meaning in the namespace.
  6. The namespace and class names that meet the naming conventions must end with ". php" to load files.
  7. Vendor NameThe namespace and class name can consist of uppercase and lowercase letters. the namespace and class name are case-sensitive to ensure compatibility between multiple systems.

PSR-1 (Basic Coding Standard)

PSR-1 is the basic encoding standard (original: official website, GitHub ). Whether the content is concise, including the class file, class name, class method name naming method.

Content:

  1. The source file must only use

  2. The encoding format of php code in the source file must only use a UTF-8 without BOM

  3. We recommend that you only declare a source file (such as a class, function, or constant) or perform operations that cause side effects (such as output information and modification. ini configuration file), but do not do both.

  4. Namespaces and classes must comply with PSR-0 standards

  5. The class name must be written in StudlyCaps.

  6. The constants in the class must only contain uppercase letters and underscores (_).

  7. The method name must be in cameCase format.

PSR-2 (Coding Style Guide)

PSR-2 is the encoding style standard (original: Official Site, GitHub ). The content is a little more, but it is relatively simple, it is based on the PSR-1, contains indentation, the length of each line of code, line feed, method visibility statement, space and method body braces wrap related provisions.

Standard content:

  1. The code must comply with the PSR-1

  2. The code must be indented with four spaces instead of tabs.

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

  4. There must be one empty row under the namespace declaration and one empty row under the declaration of use.

  5. The left curly braces of the class must be placed under the declaration to form a row, and the right curly braces must be placed under the class body to form a row.

  6. The left curly braces of the method must be placed under the declaration and the right curly braces must be placed in the next line of the method body.

  7. All attributes and methods must have a visibility declaration; abstract and final declarations must be prior to the visibility declaration; and static declarations must be after the visibility declaration

  8. There must be a space at the end of the structure control keyword, and there must be no space at the end of the method and function call.

  9. The left curly braces of the structure control must be placed in the same line as the structure control code body, and the right curly braces must be placed in the next line of the structure control code body.

  10. No space is allowed after the left parentheses of the control structure, and no space is allowed before the right parentheses.

The PSR-3 (Logger Interface)

PSR-3 is the definition of the application log class through the interface (original: official website, GitHub ). The content is very simple, it is an interface, just reference the official sample code. 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 to record logs of eight levels (debug, info, notice, warning, error, critical, alert, emergency.

  2. The ninth method is log, which accepts the log level as the first parameter. To call this method with a log level constant, it must be consistent with the result of directly calling the specified level method. To call this method with a Log level that is not defined in this specification and is not known to the specific implementation, you must throw a Psr \ Log \ InvalidArgumentException. Custom log levels are not recommended unless you are very sure that the current class library supports them.

PSR-4 (Improved Autoloading)

PSR-4 is the domestic version of the automatic loading specification (original: Official Site, GitHub ). It is a replacement for PSR-0 specifications. It can be compatible with any other automatic 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 be similar to the following example: ( )*
    1. A fully qualified class Name must have a top-level namespace (Vendor Name)

    2. A fully qualified class name can have multiple sub-namespaces

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

    4. Underlines have no special meaning in fully qualified class names.

    5. Letters can be a combination of uppercase and lowercase letters in fully qualified class names.

    6. All class names must be referenced in uppercase and lowercase letters.

  3. When a file is loaded from a fully qualified class name:
    1. In fully qualified class names, the prefix of one or more consecutive sub-namespaces (excluding the delimiters of top-level namespaces) corresponds to at least one base directory.

    2. The name of a consecutive sub-namespace after the "namespace prefix" corresponds to a sub-directory under the "basic directory", where the namespace separator represents the directory separator. The subdirectory name must be in the same case as the namespace name.

    3. The termination class name corresponds to a file ending with. php. The file name must match the ending class name in case.

  4. The implementation of the automatic loader cannot throw any exceptions, and cannot cause any errors of any level; nor should it return values.

Note: for specific provisions, refer to other Chinese translation versions.

The above section describes the content of PHP's series of SRS, including related content. I hope my friends who are interested in PHP tutorials will be helpful.

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.