Basic code Specification This specification formulates the relevant standards for the basic elements of Code, To ensure a high degree of technical interoperability between shared PHP code. Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"), "Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"), A detailed description of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in [RFC 2119][]. Overview The PHP code file must be PHP code files must be UTF-8 encoded without a BOM; 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; Namespaces and classes must conform to the automatic loading specification for PSR: psr-0[]; 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. File 2.1. PHP tagsPHP code must use long tags or short output labels; You must not use other custom tags. 2.2. Character encodingThe PHP code must be and can only be encoded with UTF-8 without a BOM. 2.3. Dependency effect (side effects)A php file should either define only new declarations, such as classes, functions, or constants that do not have a subordinate effect, or only logical operations that produce a subordinate effect, but not both. The term "subordination effect" (side effects) means that simply by including a file, you do not directly declare the class, Functions, constants, and so on, while performing logical operations. The "dependent effects" include, but are not limited to, generating output, direct require or include, connecting to external services, modifying INI configuration, throwing errors or exceptions, modifying global or static variables, reading or writing files, and so on. Here is a counter example, a copy of the code that contains the declaration and produces the dependency effect:
- Dependency effect: Modifying INI configuration
- Ini_set (' error_reporting ', e_all);
- Dependency Effect: Introducing files
- Include "file.php";
- Dependency effect: Generate output
- echo "\ n";
- declaring functions
- function foo ()
- {
- function Body part
- }
Copy CodeThe following is an example of a code that contains only declarations that do not have a dependency effect:
- declaring functions
- function foo ()
- {
- function Body part
- }
- Conditional Declaration * * Not a subordinate effect
- if (! function_exists (' Bar ')) {
- function Bar ()
- {
- function Body part
- }
- }
Copy CodeNamespaces and classes Namespaces and the naming of classes must follow [psr-0][]. By specification, each class is independent of one file, and the namespace has at least one hierarchy: the top-level organization name (vendor name). The name of the class must follow the hump naming specification at the beginning of studlycaps capitalization. PHP 5.3 and later versions of the code must use a formal namespace. For example:
- PHP 5.3 and later versions of the notation
- namespace Vendor\model;
- Class Foo
- {
- }
Copy CodeThe 5.2.x and previous versions should use the pseudo-namespace notation, which is conventionally used with the top-level organization name (vendor name), such as Vendor_, as the class prefix.
- 5.2.x and previous versions of the notation
- Class Vendor_model_foo
- {
- }
Copy CodeConstants, properties, and methods of a class The "class" here refers to all classes, interfaces, and reusable code blocks (traits) 4.1. ConstantsAll letters in a class's constants must be capitalized, and the words are underlined. Refer to the following code:
- namespace Vendor\model;
- Class Foo
- {
- Const VERSION = ' 1.0 ';
- Const date_approved = ' 2012-06-01 ';
- }
Copy Code4.2. PropertiesA class's property name 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 require enforcement, but regardless of which naming method you follow, you should remain consistent within a certain range. This range can be the entire team, the entire package, the entire class, or the entire method. 4.3. MethodsThe method name must conform to the CamelCase ()-style lowercase start hump naming specification. Turn from GitHub (Pizzaliu) |