[PHP] A brief description of the PSR specification

Source: Internet
Author: User
[PHP] A brief description of the PSR specification

This is a series of php Development specifications, which are divided into several versions and are relatively superficial. However, we still hope to view the specifications from time to time. in order to facilitate memory and follow-up, I picked up the keywords as necessary and made a simple and necessary standard record. (It is a brick-and-mortar ...)
Https://github.com/PizzaLiu/PHP-FIG
Http://segmentfault.com/a/1190000002521577

Officially released file version

PSR-0 (deprecated)

PSR-1 basic code specification

PSR-2 code style specification

PSR-2 Supplemental Documentation

Specification for PSR-3 log interfaces

PSR-4 automatic loading

PSR-5 and PSR-6 vote not passed

Must PSR-1 basic code specification

The php code file must be

PHP code files must be encoded with a UTF-8 without BOM;

The namespace and the class must comply with the automatic loading specification of the PSR: one of the PSR-0 or PSR-4;

Class names must follow the hump naming rules starting with StudlyCaps capital;

All the letters of a constant in the class must be uppercase, and words must be separated by underscores;

The method name must comply with camelCase-style lower-case start camelease naming rules.

PHP code is required Long tag or Short output tags; other custom tags are not allowed.

The namespace and the class name must follow the PSR-0.

PHP 5.3 and later versions of code must use a formal namespace.

Each class is an independent file with at least one namespace level: top-level organization name)

PSR-2 code style specification

The code must follow the encoding specification in the PSR-1.

The code must use four space characters instead of the tab key for indentation.

Note: the advantage of using spaces instead of tab indentation is to avoid confusion when comparing code differences, patching, rereading code and comments. In addition, space indentation makes alignment more convenient.

A blank row must be inserted after each namespace declaration statement and use declaration statement block.

The start curly braces ({) of the class must be written in the function declaration and a row of the class. the ending curly braces (}) must also be written in the main body of the function.

The start curly braces ({) of the method must be written in the function declaration, and the end curly braces (}) must also be written in the main body of the function.

The class attributes and methods must be added with access modifiers (private, protected, and public). abstract and final must be declared before the access modifier, while static must be declared after the access modifier.

There must be a space character after the keyword of the control structure, but not when calling a method or function.

The starting curly braces ({) of the control structure must be written in the same row of the statement, and the ending curly braces (}) must be written in the same row after the subject.

No space character is allowed between the start and left parentheses of the control structure and the end of the right parenthesis.

All php files must use Unix LF (linefeed) as the row Terminator.

All php files must end with a blank line.

The final PHP code file must be omitted?> End tag.

There must be no more than one statement in each row.

No extra space characters are allowed after non-empty rows.

All PHP keywords must be in lowercase.

Constants true, false, and null must all be in lowercase.

PSR-3 log interface specification PSR-4 automatic loading specification

A complete class name must have the following structure:

  1. \ <命名空间> (\ <子命名空间> )*\ <类名>
  2. The complete class name must have a top-level namespace called "vendor namespace ";
  3. A complete class name can have one or more sub-namespaces;
  4. The complete class name must have a final class name;
  5. Any part of the complete class name has no special meanings;
  6. The complete class name can contain any uppercase/lowercase letter;
  7. All class names must be case sensitive.

When the corresponding file is loaded according to the complete class name

  1. In the complete class name, remove the first namespace separator. one or more consecutive namespaces and subnamespaces are used as the "namespace prefix ", it must correspond to at least one "file base directory;
  2. The sub-namespace that follows the namespace prefix must match the corresponding "file Base Directory". The namespace separator is used as the directory separator.
  3. The class name at the end must be the same as the corresponding file suffixed with. php.
  4. The implementation of the automatic loader must not throw an exception, trigger any level of error information, and return values.

Recommended (for more details) PSR-1

In PHP code, only declarations such as classes, functions, and constants should be defined, or other operations that will produce subordination effects (such as generating file output and modifying. ini configuration file), either of which can be selected;

Class attribute names can follow the upper-case camelCase ($ StudlyCaps), lower-case camelCase ($ camelCase), or underline ($ under_score ), this specification is not mandatory, but no matter which naming method is followed, it should be consistent within a certain range.

PSR-2

The number of characters in each line should be kept within 80. Theoretically, there must be no more than 120 characters, but there must be no hard limit.

Empty lines make it easier to read code and facilitate code chunks.

Code sample PSR-2

  1. Namespace Vendor \ Package;
  2. Use FooInterface;
  3. Use BarClass as Bar;
  4. Use OtherVendor \ OtherPackage \ BazClass;
  5. Class Foo extends Bar implements FooInterface
  6. {
  7. Public function sampleFunction ($ a, $ B = null)
  8. {
  9. If ($ a ===$ B ){
  10. Bar ();
  11. } Elseif ($ a> $ B ){
  12. $ Foo-> bar ($ arg1 );
  13. } Else {
  14. BazClass: bar ($ arg2, $ arg3 );
  15. }
  16. }
  17. Final public static function bar ()
  18. {
  19. // Method body
  20. }
  21. }

PHP, PSR

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.