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 versionPSR-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 specificationThe 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 specificationThe 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 specificationA complete class name must have the following structure:
- \ <命名空间> (\ <子命名空间> )*\ <类名>
- The complete class name must have a top-level namespace called "vendor namespace ";
- A complete class name can have one or more sub-namespaces;
- The complete class name must have a final class name;
- Any part of the complete class name has no special meanings;
- The complete class name can contain any uppercase/lowercase letter;
- All class names must be case sensitive.
When the corresponding file is loaded according to the complete class name
- 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;
- The sub-namespace that follows the namespace prefix must match the corresponding "file Base Directory". The namespace separator is used as the directory separator.
- The class name at the end must be the same as the corresponding file suffixed with. php.
- 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-1In 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-2The 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
- Namespace Vendor \ Package;
- Use FooInterface;
- Use BarClass as Bar;
- Use OtherVendor \ OtherPackage \ BazClass;
- Class Foo extends Bar implements FooInterface
- {
- Public function sampleFunction ($ a, $ B = null)
- {
- If ($ a ===$ B ){
- Bar ();
- } Elseif ($ a> $ B ){
- $ Foo-> bar ($ arg1 );
- } Else {
- BazClass: bar ($ arg2, $ arg3 );
- }
- }
- Final public static function bar ()
- {
- // Method body
- }
- }
|