Code Style Specification This specification is the inheritance and extension of the [psr-1][] Basic code specification. This specification hopes to reduce the inconvenience caused by different code styles when browsing different authors ' code by developing a series of rules for standardizing PHP code. When multiple programmers work together on multiple projects, a common coding specification is required, The style specification in this article derives from the common characteristics of many different project code styles, Therefore, the value of this specification is that we all follow this coding style, not in itself. 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 code must follow the encoding specification in [psr-1][]. The code must be indented using 4 space characters instead of the TAB key. The number of characters per line should remain soft within 80, theoretically must not be more than 120, but must not have a hard limit. 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 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. 1.1. ExampleThe following example program simply shows most of the above specifications:
- 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 CodeGeneral clauses 2.1 Basic Coding GuidelinesThe code must conform to all specifications in [psr-1][]. 2.2 FilesAll 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. 2.3. LineThe length of the line must not be rigidly constrained. Soft length constraints must be limited to 120 characters, if beyond this length, the editor with code specification check must issue a warning, but must not issue an error prompt. Each line should not be more than 80 characters, and a line greater than 80 characters should be folded into multiple lines. There must be no extra whitespace after the non-empty line. Blank lines make it easier to read code and help with the code's chunking. There must be more than one statement per line. 2.4. IndentThe code must be indented with 4 spaces and must not use the TAB key. Note: The advantage of using a space instead of the tab indent is that Avoid confusion when comparing code differences, patching, re-reading code, and commenting. Also, use a space indent to make alignment easier. 2.5. Keywords and true/false/nullall PHP [keywords] [] must be all lowercase. Constants True, FALSE, and null must all be lowercase. namespace and use declarations A blank line must be inserted after the namespace declaration. All use must be declared after namespace. Each use declaration statement must have only one use keyword. You must have a blank line after the use declaration statement block. For example:
- namespace Vendor\package;
- Use Fooclass;
- Use Barclass as Bar;
- Use Othervendor\otherpackage\bazclass;
- ... additional PHP code ...
Copy CodeClasses, properties, and methods The "class" Here refers to all class classes, interfaces, and traits reusable blocks of code. 4.1. Extensions and InheritanceKeywords extends and implements must be written on the same line as the class name. The opening curly brace of a class must be exclusive to one line, and the closing curly brace must also be exclusive on the class body.
- namespace Vendor\package;
- Use Fooclass;
- Use Barclass as Bar;
- Use Othervendor\otherpackage\bazclass;
- Class ClassName extends ParentClass implements \arrayaccess, \countable
- {
- Constants, properties, methods
- }
Copy CodeThe Implements inheritance list can also be divided into multiple lines, so that each inherited interface name must be separated into separate rows, including the first one.
- namespace Vendor\package;
- Use Fooclass;
- Use Barclass as Bar;
- Use Othervendor\otherpackage\bazclass;
- Class ClassName extends ParentClass implements
- \arrayaccess,
- \countable,
- \serializable
- {
- Constants, properties, methods
- }
Copy Code4.2. PropertiesEach property must have an access modifier added. You must not declare a property using the keyword var. Each statement must not have more than one attribute defined. do not use underscores as prefixes to differentiate whether the attribute is protected or private. The following is an example of a property declaration:
- namespace Vendor\package;
- Class ClassName
- {
- public $foo = null;
- }
Copy Code4.3. MethodsAll methods must have an access modifier added. do not use underscores as prefixes to differentiate whether the method is protected or private. The method name must not have a space character after it, its opening curly braces have to be exclusive, and the closing curly braces must also be separated into a line after the method body. The argument must not have spaces before the left parenthesis and the closing parenthesis. A standard method declaration can refer to the following example, paying attention to its parentheses, commas, spaces, and the position of the curly braces.
- namespace Vendor\package;
- Class ClassName
- {
- Public Function Foobarbaz ($arg 1, & $arg 2, $arg 3 = [])
- {
- Method body
- }
- }
Copy Code4.4. Parameters of the methodIn the argument list, you must have a space after each comma, and no space in front of the comma. Parameters that have default values must be placed at the end of the parameter list.
- namespace Vendor\package;
- Class ClassName
- {
- Public Function foo ($arg 1, & $arg 2, $arg 3 = [])
- {
- Method body
- }
- }
Copy CodeThe parameter list can be broken into multiple rows so that each parameter, including the first parameter, must be taken separately. After splitting the argument list into multiple rows, the closing parenthesis and the method start curly braces must be written on the same line, separated by a space.
- namespace Vendor\package;
- Class ClassName
- {
- Public Function Averylongmethodname (
- Classtypehint $arg 1,
- & $arg 2,
- Array $arg 3 = []
- ) {
- Method body
- }
- }
Copy Code4.5. Abstract, final, and staticWhen an abstract or final declaration needs to be added, it must be written before the access modifier, and static must be written thereafter.
- namespace Vendor\package;
- Abstract class ClassName
- {
- protected static $foo;
- Abstract protected function Zim ();
- Final public static function bar ()
- {
- Method body
- }
- }
Copy Code4.6. Methods and function callsMethod and function call, there must be no space between the method name or the function name and the argument opening parenthesis, and there must be no spaces before the argument closing parenthesis. There must be no space before each parameter, but a space is required thereafter.
- Bar ();
- $foo->bar ($arg 1);
- Foo::bar ($arg 2, $arg 3);
Copy CodeParameters can be broken into multiple rows, and each parameter, including the first parameter, must be taken separately.
- $foo->bar (
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- );
Copy CodeControl structure The basic specifications of the control structure are as follows: Control structure Keyword must have a space. Opening parenthesis ( must not have spaces after.) There must be no spaces before the closing parenthesis. Closing parenthesis) must have a space between the opening curly braces {. The body of a struct must be indented once. Closing curly Brackets} must be in the body of the structure after the individual row. The body of each struct must be enclosed in pairs of curly braces, This allows the structure to be more structured and reduces the likelihood of errors when new rows are added. 5.1. If, ElseIf, and ElseThe standard if structure is shown in the following code, looking at the brackets, spaces, and the position of the curly braces, Note that else and ElseIf are all on the same line as the preceding closing curly braces.
- if ($expr 1) {
- If body
- } elseif ($expr 2) {
- ElseIf body
- } else {
- else body;
- }
Copy CodeThe keyword ElseIf should be used instead of all else if, so that all control keywords are like a single word. 5.2. Switch and caseThe standard switch structure is shown in the following code, looking for brackets, spaces, and the position of curly braces. The case statement must be indented relative to the switch, and the break statement and other statements within the case must be indented relative to the case. If there is a non-empty case straight-through statement, the body must have a comment like//no break.
- Switch ($expr) {
- Case 0:
- Echo ' first case, with a break ';
- Break
- Case 1:
- Echo ' Second case, which falls through ';
- No break
- Case 2:
- Case 3:
- Case 4:
- Echo ' third case, return instead of break ';
- Return
- Default
- Echo ' Default case ';
- Break
- }
Copy Code5.3. While and do whileA canonical while statement should look like this, with parentheses, spaces, and the position of curly braces.
- while ($expr) {
- Structure body
- }
Copy CodeThe standard do-while statement looks like this, and, again, note the position of the brackets, spaces, and curly braces.
- do {
- Structure body;
- } while ($expr);
Copy Code5.4. ForThe standard for statement looks like this, paying attention to its parentheses, spaces, and the position of the curly braces.
- for ($i = 0; $i < $i + +) {
- For body
- }
Copy Code5.5. foreachThe standard foreach statement is as follows, paying attention to its parentheses, spaces, and the position of the curly braces.
- foreach ($iterable as $key = = $value) {
- foreach Body
- }
Copy Code5.6. Try, catchThe standard try Catch statement looks like this, paying attention to its parentheses, spaces, and the position of the curly braces.
- try {
- Try Body
- } catch (Firstexceptiontype $e) {
- Catch body
- } catch (Otherexceptiontype $e) {
- Catch body
- }
Copy CodeClosed Package When you declare a closure, you must have a space after the keyword function and before and after the keyword use. The opening curly brace must be written on the same line as the declaration, and the closing brace must follow the next line at the end of the body. There must be no spaces after the left parenthesis of the argument list and the variable list, and before the closing parenthesis. In the arguments and variables list, there must be no spaces before commas, and spaces must be followed by commas. Parameters with default values in closures must be placed behind the list. The standard closure declaration statement is as follows, noting its parentheses, commas, spaces, and the position of the curly braces.
- $closureWithArgs = function ($arg 1, $arg 2) {
- Body
- };
- $closureWithArgsAndVars = function ($arg 1, $arg 2) use ($var 1, $var 2) {
- Body
- };
Copy CodeThe argument list and the list of variables can be divided into multiple rows, so that each parameter or variable, including the first one, must be taken separately, and the closing parenthesis of the list must be placed on the same line as the opening brace of the closure. Here are a few examples of multiple cases where the list of parameters and variables is divided into multiple rows.
- $longArgs _novars = function (
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) {
- Body
- };
- $noArgs _longvars = function () use (
- $longVar 1,
- $longerVar 2,
- $muchLongerVar 3
- ) {
- Body
- };
- $longArgs _longvars = function (
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) Use (
- $longVar 1,
- $longerVar 2,
- $muchLongerVar 3
- ) {
- Body
- };
- $longArgs _shortvars = function (
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) use ($var 1) {
- Body
- };
- $shortArgs _longvars = function ($arg) use (
- $longVar 1,
- $longerVar 2,
- $muchLongerVar 3
- ) {
- Body
- };
Copy CodeNote that the above rules still apply when a closure is used directly as a parameter to a function or method call.
- $foo->bar (
- $arg 1,
- function ($arg 2) use ($var 1) {
- Body
- },
- $arg 3
- );
Copy CodeSummarize The above specifications are inevitably negligent, including but not limited to: Definition of global variables and constants Definition of a function Operators and Assignments inline alignment Comments and Document description blocks prefix and suffix of class name Best practices Revisions and extensions after this specification will compensate for these deficiencies. Appendix A. Questionnaire SurveyIn order to prepare the code, the Panel has developed a questionnaire that is used to count the common norms of each member project. The following is the data for this survey, which is available for review. A.1. Questionnaire data
- url,http://www.horde.org/apps/horde/docs/coding_standards,http://pear.php.net/manual/en/standards.php,http:// solarphp.com/manual/appendix-standards.style,http://framework.zend.com/manual/en/coding-standard.html,http:// symfony.com/doc/2.0/contributing/code/standards.html,http://www.ppi.io/docs/coding-standards.html,https:// github.com/ezsystems/ezp-next/wiki/codingstandards,http://book.cakephp.org/2.0/en/contributing/ cakephp-coding-conventions.html,https://github.com/unionofrad/lithium/wiki/spec%3a-coding,http://drupal.org/ Coding-standards,http://code.google.com/p/sabredav/,http://area51.phpbb.com/docs/31x/coding-guidelines.html, Https://docs.google.com/a/zikula.org/document/edit?authkey=CPCU0Us&hgd=1&id=1fcqb93Sn-hR9c0mkN6m_ tywnmevoswkbtsc0tkkzmja,http://www.chisimba.com,n/a,https://github.com/respect/project-info/blob/master/ Coding-standards-sample.php,n/a,object calisthenics for php,http://doc.nette.org/en/coding-standard,http:// Flow3.typo3.org,https://github.com/propelorm/propEl2/wiki/coding-standards,http://developer.joomla.org/coding-standards.html
- Voting,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,no,no,no,?, Yes,no,yes
- Indent_type,4,4,4,4,4,tab,4,tab,tab,2,4,tab,4,4,4,4,4,4,tab,tab,4,tab
- line_length_limit_soft,75,75,75,75,no,85,120,120,80,80,80,no,100,80,80,?,?, 120,80,120,no,150
- line_length_limit_hard,85,85,85,85,no,no,no,no,100,?, no,no,no,100,100,?, 120,120,no,no,no,no
- class_names,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,lower_under,studly, lower,studly,studly,studly,studly,?, studly,studly,studly
- Class_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,next,next,next,next,next, Next,same,next,next
- Constant_names,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper , Upper,upper,upper,upper,upper,upper
- True_false_null,lower,lower,lower,lower,lower,lower,lower,lower,lower,upper,lower,lower,lower,upper,lower, Lower,lower,lower,lower,upper,lower,lower
- Method_names,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,lower_under,camel,camel,camel, Camel,camel,camel,camel,camel,camel,camel
- Method_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,same,next,next,next,next, Next,same,next,next
- Control_brace_line,same,same,same,same,same,same,next,same,same,same,same,next,same,same,next,same,same,same, Same,same,same,next
- Control_space_after,yes,yes,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes
- Always_use_control_braces,yes,yes,yes,yes,yes,yes,no,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yes
- Else_elseif_line,same,same,same,same,same,same,next,same,same,next,same,next,same,next,next,same,same,same, Same,same,same,next
- case_break_indent_from_switch,0/1,0/1,0/1,1/2,1/2,1/2,1/2,1/1,1/1,1/2,1/2,1/1,1/2,1/2,1/2,1/2,1/2,1/2,0/1,1/ 1,1/2,1/2
- Function_space_after,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no
- Closing_php_tag_required,no,no,no,no,no,no,no,no,yes,no,no,no,no,yes,no,no,no,no,no,yes,no,no
- LINE_ENDINGS,LF,LF,LF,LF,LF,LF,LF,LF,?, LF,?, lf,lf,lf,lf,?,, LF,?, LF,LF,LF
- Static_or_visibility_first,static,?, static,either,either,either,visibility,visibility,visibility,either,static , either,?, visibility,?,?, either,either,visibility,visibility,static,?
- Control_space_parens,no,no,no,no,no,no,yes,no,no,no,no,no,no,yes,?, No,no,no,no,no,no,no
- Blank_line_after_php,no,no,no,no,yes,no,no,no,no,yes,yes,no,no,yes,?, Yes,yes,no,yes,no,yes,no
- class_method_control_brace,next/next/same,next/next/same,next/next/same,next/next/same,next/next/same,same/ same/same,next/next/next,same/same/same,same/same/same,same/same/same,same/same/same,next/next/next,next/next/ Same,next/same/same,next/next/next,next/next/same,next/next/same,next/next/same,next/next/same,same/same/same, Next/next/same,next/next/next
Copy CodeA.2. Questionnaire descriptionIndent_type: The indentation type. tab = "Use Tab key Once", 2 or 4 = "Number of spaces" Line_length_limit_soft: The "soft" limit for the number of characters per line. ? = Non-argument or no answer, no means no limit. Line_length_limit_hard: The "hard" limit for the number of characters per line. ? = Non-argument or no answer, no means no limit. Class_names: The naming of the class name. lower = only lowercase letters are allowed, Lower_under = lower-case letters, studly = Studlycase hump style. Class_brace_line: The opening curly brace of a class is in the same line as the Class keyword or on its next line? Constant_names: How are the constants of the class named? Upper = uppercase letters separated by underscores. True_false_null: Is the keyword true, false, and null all lowercase lower or all uppercase upper? Method_names: How is the method name named? Camel = CamelCase, Lower_under = lowercase letters separated by underscores. Method_brace_line: Does the opening curly brace of a method be the same line as the method name or the next line in it? Control_brace_line: Is the opening curly brace of the control structure the same line as the declaration or the next line in it? Control_space_after: Is there a space after controlling the structure keyword? Always_use_control_braces: Does the control structure have to be enclosed within curly braces? Else_elseif_line: else or ElseIf with the preceding closing curly brace on the same line or in its next line? Case_break_indent_from_switch: How many times does the case and break in the switch statement need to be indented relative to the switch? Function_space_after: Is there a space between the function name and the opening parenthesis of the variable list in the function call statement? Closing_php_tag_required: Pure PHP code files, do I need to?> the end tag? Line_endings: Which type of line terminator is selected? Static_or_visibility_first: When declaring a static method, is static before or after the write access modifier? Control_space_parens: In the control structure, are there spaces in the left parenthesis and before the closing parenthesis? Yes = if ($expr), no = if ($expr). blank_line_after_php: Does PHP need a blank line after starting the tag? Class_method_control_brace: Start the curly braces in the position statistics for classes, methods, and control structures. A.3. Statistical results of questionnaires
- Indent_type:
- Tab:7
- 2:1
- 4:14
- Line_length_limit_soft:
- ?: 2
- No:3
- 75:4
- 80:6
- 85:1
- 100:1
- 120:4
- 150:1
- Line_length_limit_hard:
- ?: 2
- No:11
- 85:4
- 100:3
- 120:2
- Class_names:
- ?: 1
- Lower:1
- Lower_under:1
- Studly:19
- Class_brace_line:
- Next:16
- Same:6
- Constant_names:
- Upper:22
- True_false_null:
- Lower:19
- Upper:3
- Method_names:
- Camel:21
- Lower_under:1
- Method_brace_line:
- Next:15
- Same:7
- Control_brace_line:
- Next:4
- Same:18
- Control_space_after:
- No:2
- Yes:20
- Always_use_control_braces:
- No:3
- Yes:19
- Else_elseif_line:
- Next:6
- Same:16
- Case_break_indent_from_switch:
- 0/1:4
- 1/1:4
- 1/2:14
- Function_space_after:
- No:22
- Closing_php_tag_required:
- No:19
- Yes:3
- Line_endings:
- ?: 5
- Lf:17
- Static_or_visibility_first:
- ?: 5
- Either:7
- Static:4
- Visibility:6
- Control_space_parens:
- ?: 1
- No:19
- Yes:2
- blank_line_after_php:
- ?: 1
- No:13
- Yes:8
- Class_method_control_brace:
- Next/next/next:4
- Next/next/same:11
- Next/same/same:1
- Same/same/same:6
Copy CodeTurn from GitHub (Pizzaliu) |