Basic code Specification
This specification develops 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"),
Detailed descriptions of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in RFC 2119.
1. Overview
PHP code files must <?php
start with or <?=
tag;
The PHP code file must be 不带BOM的 UTF-8
encoded;
PHP code should only define classes, functions, constants, and other declarations, or other 从属效应
operations (such as generating file output and modifying. ini configuration files), which can only be selected;
Namespaces and classes must conform to the automatic loading specification for PSR: PSR-0 or PSR-4;
The name of the class must follow StudlyCaps
the Hump naming specification at the beginning of the capitalization;
Constants in a class all letters must be capitalized, and the words are separated by underscores;
The method name must conform camelCase
to the lowercase start of the hump-naming specification.
2. Document 2.1. PHP tags
PHP code must use <?php ?>
long tags or <?= ?>
short output labels;
You must not use other custom tags.
2.2. Character encoding
The PHP code must be used and only 不带BOM的UTF-8
encoded.
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 logical operations are performed only by including files, without directly declaring classes, functions, and constants.
The "dependent effect" includes, but is not limited to, generating output, directly require
or include
connecting 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:
<?php//Dependency Effect: Modify INI configuration ini_set (' error_reporting ', e_all);//Dependency effect: Introduce file include "file.php";//Dependency effect: Generate Output echo "
The following is an example of a code that contains only declarations that do not have a dependency effect:
<?php//Declare Function foo () { ///function body part}//Condition Declaration * * Not subordinate effect if (! function_exists (' Bar ')) { function bar () { //function body part }}
3. Namespaces and ClassesNamespaces and the naming of classes must follow PSR-0. (PSR-0 obsolete)
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 StudlyCaps
the Hump naming specification at the beginning of the capitalization.
PHP 5.3 and later versions of the code must use a formal namespace.
For example:
<?php//PHP 5.3 and later versions namespace Vendor\model;class foo{}
5.2.x and previous versions should use the pseudo-namespace notation, which is conventionally used with the top-level organization name (vendor name) as Vendor_
a class prefix.
<?php//5.2.x and previous versions of Class vendor_model_foo{}
4. Constants, properties, and methods of a classThe "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:
<?phpnamespace vendor\model;class foo{ Const VERSION = ' 1.0 '; Const date_approved = ' 2012-06-01 ';}
4.2. PropertiesThe property naming of a class can follow the camel-like () $StudlyCaps
, lower-case-beginning camel () $camelCase
or underscore-delimited ( $under_score
), but this specification does not impose a mandatory requirement, but it should be consistent within a certain scope regardless of which naming method is followed. This range can be the entire team, the entire package, the entire class, or the entire method.
4.3. MethodsThe method name must conform camelCase()
to the lowercase start of the hump-naming specification.
PHP PSR-1 Basic Code Specification (Chinese version)