| 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] [].
Overview
PHP code fileRequiredTo
PHP code fileRequiredIt is encoded with a UTF-8 without BOM;
PHP codeShouldOnly declarations such as classes, functions, and constants are defined, or other operations that will produce a subordinate effect (for example, generating file output and modifying the. ini configuration file). either of them can be selected;
Namespaces and classesRequiredCompliant with the automatic loading specification of the PSR: one of the PSR-0;
Class nameRequiredFollow the hump naming rules starting with StudlyCaps capital;
All the letters of constants in the class areRequiredUppercase: words are separated by underscores;
Method nameRequiredComplies with camelCase-based lower-case start hump naming rules.
File 2.1. PHP tag
PHP codeRequiredUse Long tag or Short output tag; Must notUse other custom tags. 2.2. Character encoding
PHP codeRequiredAnd only UTF-8 code without BOM can be used. 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" is not limited: generate output, direct require or include, connect to external services, modify ini configuration, 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:
- // Subordinate effect: modify the ini configuration
- Ini_set ('error _ report', E_ALL );
- // Subordinate effect: Introduce the file
- Include "file. php ";
- // Subordinate effect: generate output
- Echo"\ N ";
- // Declare a function
- Function foo ()
- {
- // Main part of the function
- }
The following is an example of a code that only contains declarations that do not produce a subordination effect:
- // Declare a function
- Function foo ()
- {
- // Main part of the function
- }
- // The condition declaration ** no ** is a subordinate effect.
- If (! Function_exists ('bar ')){
- Function bar ()
- {
- // Main part of the function
- }
- }
Namespaces and classes
The 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 ).
Class names must follow the hump naming rules starting with StudlyCaps in upper case.
Code for PHP 5.3 and later versionsRequiredUse the formal namespace.
For example:
- // PHP 5.3 and later versions
- Namespace Vendor \ Model;
- Class Foo
- {
- }
5.2.x and earlier versionsShouldThe use of pseudo namespace is generally the use of top-level organization names (vendor name), such as the class prefix of Vendor.
- // 5.2.x and earlier versions
- Class Vendor_Model_Foo
- {
- }
Class constants, attributes, and methods
The "class" here refers to all classes, interfaces, and reusable code blocks (traits) 4.1. constant
All the letters in the constant of the class areRequiredIn upper case, the words are separated by dashes.
Refer to the following code:
- Namespace Vendor \ Model;
- Class Foo
- {
- Const VERSION = '1. 0 ';
- Const DATE_APPROVED = '2017-06-01 ';
- }
4.2. attributes
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 followedShouldIn a certain range. This scope can be the entire team, the entire package, the entire class, or the entire method. 4.3. method
Method nameRequiredComplies with camelCase ()-based lower-case start hump naming rules.
|