"PHP" PSR Brief specification

Source: Internet
Author: User
Tags autoloader

PSR is a series of PHP development specifications, divided into several versions, their own learning is also relatively superficial, but still want to be able to review the specification, in order to facilitate memory and follow, I put the key words for the necessary to pick up, do a simple necessary standard records. (It's a brick-moving ... )
Https://github.com/PizzaLiu/PHP-FIG
http://segmentfault.com/a/1190000002521577

Official release file version

PSR-0 (deprecated)

PSR-1 Basic Code specification

PSR-2 Code Style Specification

PSR-2 Supplemental Documentation

PSR-3 Log Interface Specification

PSR-4 Automatic loading

PSR-5 and PSR-6 votes have not yet passed.

Basic code specification must be PSR-1

The PHP code file must be

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

Namespaces and classes must conform to the automatic loading specification for PSR: PSR-0 or PSR-4;

The name of the class must follow the hump naming specification at the beginning of studlycaps capitalization;

Constants in a class all letters must be capitalized, and the words are separated by underscores;

The method name must conform to the CamelCase-style lowercase start hump naming specification.

The PHP code must use a long label or a short output label, and must not use other custom tags.

Namespaces and the naming of classes must follow PSR-0.

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

Each class is independent of one file, and the namespace has at least one hierarchy: top-level organization name (vendor name)

PSR-2 Code Style Specification

The code must follow the coding specifications in PSR-1.

The code must be indented using 4 space characters instead of the TAB key.

Note: The advantage of using a space instead of a tab indent is to avoid confusion when comparing code differences, patching, re-reading code, and commenting. Also, use a space indent to make alignment easier.

After each namespace namespace declaration statement and the use declaration statement block, you must insert a blank line.

The opening curly brace ({) of a class must be written as a line after the function declaration, and the closing curly brace (}) must also be written in a row after the function body.

The opening curly brace ({) of the method must be written in a line after the function declaration, and the closing curly brace (}) must also be written in a row after the function body.

The properties and methods of the class must add access modifiers (private, protected, and public), abstract and final must be declared before the access modifier, and static must be declared after the access modifier.

You must have a space character after the key for the control structure, but you cannot call a method or function.

The opening curly brace ({) of the control structure must be written on the same line as the declaration, and the closing curly brace (}) must be written in a row after the body.

There must be no whitespace at the beginning of the control structure, after the opening parenthesis, and before the closing parenthesis.

All PHP files must use the Unix LF (linefeed) as the line terminator.

All PHP files must end with a blank line.

The pure PHP code file must omit the last?> end tag.

There must be more than one statement per line.

There must be no extra whitespace after the non-empty line.

All keywords in PHP must be all lowercase.

Constants True, FALSE, and null must all be lowercase.

PSR-3 Log Interface Specification PSR-4 automatic loading specification

A complete class name needs to have the following structure:

    1. \ <命名空间> (\ <子命名空间> )*\ <类名>
    2. The complete class name must have a top-level namespace, known as "Vendor namespace";
    3. The full class name can have one or more sub-namespaces;
    4. The complete class name must have a final class name;
    5. The glide line in any part of the complete class name has no special meaning;
    6. The complete class name can consist of any uppercase and lowercase letters;
    7. All class names must be case-sensitive.
Copy Code

When loading the corresponding file according to the full class name

    1. In the complete class name, remove the first namespace delimiter, one or more contiguous namespaces and sub-namespaces as "namespace prefixes", which must correspond to at least one "file base directory";
    2. The child namespace immediately following the namespace prefix must match the corresponding file base directory, where the namespace delimiter is used as the directory delimiter.
    3. The class name at the end must have the same name as the corresponding file with the. php suffix.
    4. The implementation of the autoloader (autoloader) must not throw an exception, must not trigger any level of error information, and should not have a return value.
Copy CodeSuggestions (see detailed documentation for more) PSR-1

The PHP code should only define classes, functions, constants, and other declarations, or other actions that would have a subordinate effect (such as generating file output and modifying. ini configuration files, etc.), which can only be selected;

The property name of a class can follow a camel ($StudlyCaps) with uppercase, a camel ($camelCase) at the beginning of a lowercase, or an underscore ($under _score), and this specification does not impose a mandatory requirement, but regardless of which naming method is followed, Should remain consistent within a certain range.

PSR-2

The number of characters per line should remain soft within 80, theoretically must not be more than 120, but must not have a hard limit.

Blank lines make it easier to read code and help with the code's chunking.

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 ($arg 1);
    13. } else {
    14. Bazclass::bar ($arg 2, $arg 3);
    15. }
    16. }
    17. Final public static function bar ()
    18. {
    19. Method body
    20. }
    21. }
Copy Code
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.