PHPPSR-1 basic code specification (Chinese version ),. PHPPSR-1 basic code specification (Chinese version), basic code specification this specification has developed the relevant standards of the basic elements of the code, to ensure the sharing of PHP code with a high degree of technical intercommunication PHP PSR-1 basic code specifications (Chinese version ),
Basic code specification
This article provides standards for the basic elements of the code,
To ensure that the shared PHP code has a high degree of technical interconnectivity.
Key words: "MUST" ("MUST"), "must not/must not" ("must not"), "need" ("REQUIRED "),
"Will" ("SHALL"), "will NOT" ("shall not"), "SHOULD" ("shocould"), "shouldn't" ("shocould NOT "),
For detailed descriptions of "recommendations" ("RECOMMENDED"), "Yes" ("MAY"), and "OPTIONAL" ("OPTIONAL"), see RFC 2119.
1. Overview
PHP code fileRequiredTo Or Label start;
PHP code fileRequiredToUTF-8 without BOM
Encoding;
PHP codeShouldOnly declarations such as classes, functions, and constants are defined.Subordination
(For example, generating file output and modifying the. ini configuration file), either of them can be selected;
Namespaces and classesRequiredCompliant with automatic load specifications of the HDR: one in the PSR-0 or PSR-4;
Class nameRequiredFollowStudlyCaps
Upper-case hump naming rules;
All the letters of constants in the class areRequiredUppercase: words are separated by underscores;
Method nameRequiredYescamelCase
Type.
2. file 2.1. PHP tagPHP codeRequiredUse
Long tag or
Short output tag;
Must notUse other custom tags.
2.2. Character encodingPHP codeRequiredAnd can only be usedUTF-8 without BOM
Encoding.
2.3. subordination (side effects)A php fileShouldOtherwise, only new declarations, such as classes, functions, or constants, are defined, and only logical operations that produce the subordination effect are allowed.Shouldn'tBoth.
The word "side effects" means that classes,
Functions and constants.
The "subordinate effect" includes but is not limited to: generating output and directrequire
Orinclude
, Connect to external services, modify ini configurations, throw errors or exceptions, modify global or static variables, read or write files, etc.
The following is a counterexample, containing the declaration and code that produces the subordinate effect:
\ N "; // declare function foo () {// function Subject}
The following is an example of a code that only contains declarations that do not produce a subordination effect:
3. namespaces and classesThe namespace and the class name must follow the PSR-0.
According to the specification, each class is independent of a file, and the namespace must have at least one level: top-level organization name (vendor name ).
The class name must followStudlyCaps
Description: description of the upper case.
Code for PHP 5.3 and later versionsRequiredUse the formal namespace.
For example:
5.2.x and earlier versionsShouldThe use of pseudo namespace is a convention that uses top-level organization names (vendor names) suchVendor_
Is the class prefix.
4. constants, attributes, and methods of classesThe "class" here refers to all classes, interfaces, and reusable code blocks (traits)
4.1. constantAll the letters in the constant of the class areRequiredIn upper case, the words are separated by dashes.
Refer to the following code:
4.2. attributesClass attribute names can follow the upper-case hump ($StudlyCaps
($camelCase
) Or an underline separator ($under_score
), This specification is not mandatory, but no matter which naming method is followedShouldIn a certain range. This scope can be the entire team, the entire package, the entire class, or the entire method.
4.3. methodMethod nameRequiredYescamelCase()
Type.
Because the psr-0 has been abandoned, it is not transferred
Original article: PizzaLiu github
Http://www.bkjia.com/PHPjc/1093108.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1093108.htmlTechArticlePHP PSR-1 basic code specification (Chinese version), the basic code specification this specification has developed the relevant standards of the basic elements of the code, to ensure that the sharing of PHP code has a high degree of technical intercommunication...