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 versionPSR-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-1The 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 SpecificationThe 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 specificationA complete class name needs to have the following structure:
- \ <命名空间> (\ <子命名空间> )*\ <类名>
- The complete class name must have a top-level namespace, known as "Vendor namespace";
- The full class name can have one or more sub-namespaces;
- The complete class name must have a final class name;
- The glide line in any part of the complete class name has no special meaning;
- The complete class name can consist of any uppercase and lowercase letters;
- All class names must be case-sensitive.
Copy CodeWhen loading the corresponding file according to the full class name
- 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";
- 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.
- The class name at the end must have the same name as the corresponding file with the. php suffix.
- 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-1The 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-2The 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
- 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 ($arg 1);
- } else {
- Bazclass::bar ($arg 2, $arg 3);
- }
- }
- Final public static function bar ()
- {
- Method body
- }
- }
Copy Code |