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 coding styles when browsing the code of different authors by formulating a series of rules for standardizing PHP code.
When multiple programmers collaborate in multiple projects, a common coding specification is needed. The style specification in this article is derived from the common characteristics of the coding styles of multiple different projects. Therefore, the value of this specification lies in We all follow this coding style, not in itself.
Keywords "must" ("MUST"), "must not/must not" ("MUST NOT"), "need" ("REQUIRED"), "will" ("SHALL"), "won't" ("SHALL NOT"), "should" ("SHOULD"), "should not" ("SHOULD NOT"), "recommended" ("RECOMMENDED"), "can" ("MAY") and "optional" ("OPTIONAL") is described in detail in [RFC 2119][].
Overview
Code must follow the coding conventions in [PSR-1][].
Codemustuse 4 spaces instead of the tab key for indentation.
The number of characters per line should be softly kept within 80. Theoretically must not exceed 120, but must not have a hard limit.
After each namespace declaration and use declaration block, must insert a blank line.
The opening curly brace ({) of a class must be written on its own line after the function declaration, and the closing curly brace (}) must also be written on its own line after the function body.
The opening curly brace ({) of a method must be written on its own line after the function declaration, and the closing curly brace (}) must also be written on its own line after the function body.
Class properties and methods must add access modifiers (private, protected and public), abstract and final must be declared before the access modifier, while static must be declared after the access modifier.
There must be a space after the keyword of the control structure, but there must not be when calling a method or function.
The opening curly brace ({) of the control structure must be written on the same line as the declaration, while the closing curly brace (}) must be written on its own line after the body.
There must be no spaces after the opening left bracket and before the closing right bracket of the control structure.
1.1. Example
The following example program simply demonstrates most of the above specifications:
namespace VendorPackage; use FooInterface; use BarClass as Bar; use OtherVendorOtherPackageBazClass; { public function sampleFunction($a, $b = null) {- if ($a === $b) {
- bar();
- } elseif ($a > $b) {
- $foo-> ;bar($arg1);
- } else {
- BazClass::bar($arg2, $arg3);
- }
- }
-
- final public static function bar()
- {
- // method body
- }
- }
-
-
- Copy code
-
-
General rules
2.1 Basic Coding Guidelines
Code must comply with all specifications in [PSR-1][].
2.2 Documentation
All PHP files must use Unix LF (linefeed) as the line terminator.
All PHP files must end with a blank line.
Pure PHP code filesmustomit the final ?> closing tag.
2.3. OK
The length of the line must not have a hard limit.
The soft length constraintmustbe limited to 120 characters. If it exceeds this length, the editor with code specification checkmustissue a warning, but must notissue an error prompt.
Each line should not be more than 80 characters, and lines larger than 80 characters should be folded into multiple lines.
There must not be any extra spaces after the non-blank line.
Blank lines can make it easier to read code and help to chunk the code.
Each line must not contain more than one statement.
2.4. Indentation
Codemustuse 4 spaces for indentation, must notuse the tab key.
Note: The advantage of using spaces instead of tab keys for indentation is that avoids confusion when comparing code differences, patching, re-reading code, and comments. Also, use spaces for indentation to make alignment easier.
2.5. Keywords and True/False/Null
PHP all [keywords][]mustall lowercase.
The constants true, false and null also must be all lowercase.
namespace and use declaration
A blank line must be inserted after the namespace declaration.
All uses must be declared after namespace.
Each use statement must have only one use keyword.
There must be a blank line after the use statement block is declared.
For example:
- namespace VendorPackage;
- use FooClass;
- use BarClass as Bar;
- use OtherVendorOtherPackageBazClass;
- // ... additional PHP code ...
Copy code Classes, properties and methods
The "class" here refers to all classes, interfaces and traits reusable code blocks.
4.1. Extension and inheritance
The keywords extends and implementsmust be written on the same line as the class name.
The opening brace of a class must occupy its own line, and the closing brace must occupy its own line after the class body. ? & Lt;? Phpnamespace VendorPackage; use Fooclass; use Barclass as Bar; Parentclass Implements ArrayAccess, Countable {- // constants, Properties, Methods
- }
- Copy code
- implements’ inheritance list can also
- be split into multiple lines. In this case, each inherited interface name
- must be separated into separate lines, including the first one.
namespace VendorPackage; use FooClass;use BarClass as Bar;use OtherVendorOtherPackageBazClass; class ClassName extends ParentClass implements ArrayAccess, Countable, Serializable- {
- // constants, properties, methods
- }
- Copy code
- 4.2. Properties
- Each property
- must
- have an access modifier added.
-
- Must not
use the keyword var to declare a property.
Each statement must not define more than one attribute.
Don’t use an underscore as a prefix to distinguish whether a property is protected or private.
The following is an example of property declaration: namespace VendorPackage; class ClassName{ public $foo = null; } Copy code - 4.3. Method
- All methods
- must
- add access modifiers.
-
- Don’t
use underscore as a prefix to distinguish whether a method is protected or private.
There must be no spaces after the method name. The opening curly brace must be on its own line, and the closing curly brace must be on its own line after the method body. There must be no spaces after the left bracket and before the right bracket of the parameter.
A standard method declaration can refer to the following example, paying attention to the position of brackets, commas, spaces and curly braces. namespace VendorPackage;class ClassName{ public function fooBarBaz($arg1, &$arg2, $arg3 = []) { // method body } } Copy code 4.4. Parameters of method
In the parameter list, must have a space after each comma, and must not have a space before the comma.
Parameters with default values must be placed at the end of the parameter list. - namespace VendorPackage;
- class ClassName
- {
- public function foo($arg1, &$arg2, $arg3 = [])
- {
- // method body
- }
- }
Copy the code The parameter list can be split into multiple lines, so that each parameter, including the first parameter, must be on its own line.
After splitting the parameter list into multiple lines, the closing bracket and the opening brace of the method must be written on the same line, separated by a space in the middle. - namespace VendorPackage;
- class ClassName
- {
- public function aVeryLongMethodName(
- ClassTypeHint $arg1,
- &$arg2,
- array $arg3 = []
- ) {
- // method body
- }
- }
Copy code4.5. abstract, final, and static
When you need to add abstract or final declaration, must be written before the access modifier, and static must be written after it. - namespace VendorPackage;
- abstract class ClassName
- {
- protected static $foo;
- abstract protected function zim();
- final public static function bar()
- {
- // method body
- }
- }
Copy code4.6. Method and function calls
When calling methods and functions, there must be no spaces between the method name or function name and the left parentheses of the parameters, and there must not be spaces before the right parentheses of the parameters. Each parameter must not have a space before it, but must have a space after it. bar(); - $foo->bar($arg1);
- Foo::bar($arg2, $arg3);
-
-
Copy code
parameters OK Split into multiple lines. At this time, each parameter including the first parameter must be in a separate line.
$foo->bar( $longArgument,- $longerArgument,
- $muchLongerArgument
- );
-
-
- Copy code
Control structure
The basic specifications of the control structure are as follows:
There must be a space after the control structure keyword.
There must be no space after the left bracket (.
There must be no space before the right bracket ).
There must be a space between the right bracket ) and the opening curly bracket { .
The main body of the structure must be indented once.
The closing curly brace } must be placed on a separate line after the body of the structure.
The body of each structure must be enclosed in pairs of curly braces, This makes the structure more structured and reduces the possibility of errors when adding new lines.
5.1. if , elseif and else
The standard if structure is as shown in the following code. Pay attention to the position of brackets, spaces, and curly braces. Note that else and elseif are both on the same line as the previous closing curly brace.
if ($expr1) { // if body} elseif ($expr2) { // elseif body } else { // else body;- }
- Copy The code
- should
use the keyword elseif instead of all else if s so that all control keywords appear to be a single word.
5.2. switch and case
The standard switch structure is as shown in the following code. Pay attention to the position of brackets, spaces and curly braces. The case statement must be indented relative to the switch, while the break statement and other statements within the case must be indented relative to the case. If there is a non-empty case through statement, there must be a comment like // no break in the body. 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 Code
- 5.3. while and do while
A standardized while statement should look like the following, pay attention to the position of brackets, spaces, and curly braces. - while ($expr) {
- // structure body
- }
Copy codeThe standard do while statement is as follows. Likewise, pay attention to the brackets, spaces and flowers. The position of the brackets. - do {
- // structure body;
- } while ($expr);
Copy code 5.4. for
The standard for statement is as follows, pay attention to the position of brackets, spaces and curly braces. - for ($i = 0; $i < 10; $i++) {
- // for body
- }
Copy code 5.5. foreach
The standard foreach statement is as follows, pay attention to the position of brackets, spaces and curly braces. - foreach ($iterable as $key => $value) {
- // foreach body
- }Copy code5.6. try, catch
The standard try catch statement is as follows, pay attention to the position of brackets, spaces and curly braces.
- try {
- // try body
- } catch (FirstExceptionType $e) {
- // catch body
- } catch (OtherExceptionType $e) {
- // catch body
- }
Copy code Closure
When a closure is declared, there must be a space after the keyword function and before and after the keyword use.
The opening brace must be written on the same line as the declaration, and the closing brace must follow the line immediately following the end of the body.
There must be no spaces after the left bracket and before the right bracket in the parameter list and variable list.
In the parameter and variable lists, must not have spaces before the comma, and must have spaces after the comma.
Parameters with default values in the closure must be placed at the end of the list.
The standard closure declaration statement is as follows, pay attention to the position of parentheses, commas, spaces and curly braces. $closureWithArgs = function ($arg1, $arg2) { // body- };
- $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
- // body
- };
- Copy code
Parameter lists and variable lists can
be split into multiple lines, so that each parameter or variable, including the first one, must be on its own line, while The closing brace of the list and the opening curly brace of the closure must be placed on the same line.
The following examples include multiple situations where parameter and variable lists are divided into multiple lines. $longArgs_noVars = function ( $longArgument,- $longerArgument,
- $muchLongerArgument
- ) {
- // body
- };
- $noArgs_longVars = function () use (
- $longVar 1,
- $longerVar2,
- $muchLongerVar3
- ) {
- // body
- };
- $longArgs_longVars = function (
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) use (
- $longVar1,
- $longerVar2 ,
- $muchLongerVar3
- ) {
- // body
- };
- $longArgs_shortVars = function (
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) use ($var1) {
- // body
- };
- $shortArgs_longVars = function ( $arg) use (
- $longVar1,
- $longerVar2,
- $muchLongerVar3
- ) {
- // body
- };
- Copy code
Note that when closures are used directly as parameters for function or method calls, The above rules still apply. $foo->bar( $arg1,- function ($arg2) use ($var1) {
- // body
- },
- $arg3
- );
-
-
- Copy Code
Summary
There are inevitably some oversights in the above specifications, including but not limited to:
Definition of global variables and constants
Definition of function
Operators and assignments
Inline alignment
Comments and documentation blocks
Prefix and suffix of class name
Best Practices
Subsequent revisions and expansions of this specification will make up for the above shortcomings.
Appendix A. Questionnaire
In order to write this specification, the team developed a questionnaire to collect statistics on the common specifications of each member's project. The following is the data from this questionnaire survey, available for review here.
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 code
A.2. Questionnaire instructions
indent_type: Indent type. tab = "Use the tab key once", 2 or 4 = "Number of spaces"
line_length_limit_soft: "soft" limit on the number of characters per line. ? = no answer or no answer, no means no limit.
line_length_limit_hard: The "hard" limit on the number of characters per line. ? = no answer or no answer, no means no limit.
class_names: Naming of class names. lower = only lowercase letters allowed, lower_under = lowercase letters separated by underscores, studly = StudlyCase camel case style.
class_brace_line: Is the opening brace of the class on the same line as the class keyword or on the next line?
constant_names: How to name the constants of the class? upper = uppercase letters separated by underscores.
true_false_null: Are the keywords true, false and null all lowercase or all uppercase?
method_names: How to name method names? camel = camelCase, lower_under = lowercase letters separated by underscores.
method_brace_line: Is the opening brace of the method on the same line as the method name or on the next line?
control_brace_line: Is the opening brace of the control structure on the same line as the declaration or on the next line?
control_space_after: Is there a space after the control structure keyword?
always_use_control_braces: Are control structures always enclosed in curly braces?
else_elseif_line: else or elseif Is the else or elseif on the same line as the preceding closing curly brace or on the next line?
case_break_indent_from_switch: How many times do the case and break in the switch statement need to be indented relative to the switch?
function_space_after: In the function call statement, is there a space between the function name and the left bracket of the variable list?
closing_php_tag_required: Does a file with pure PHP code need a ?> closing tag?
line_endings: What type of line endings to choose?
static_or_visibility_first: When declaring a static method, should static be written before or after the access modifier?
control_space_parens: In the control structure, are there spaces after the left bracket and before the right bracket? yes = if ( $expr ), no = if ($expr).
blank_line_after_php: Does PHP need a blank line after the opening tag?
class_method_control_brace: Start curly brace position statistics in classes, methods and control structures.
A.3. Questionnaire statistics results 问INDENT_TYPE: TAB: 7 2: 1- 4: 14
- line_length_limit_soft:
- ?: 2
- no: 3
- 75: 4
- 85: 1
- 100 100 : 1
- 120: 4
- 150: 1
- line_length_limit_hard:
- ?: 2
- no: 11
- 85: 4
- 100: 3
- 120: 2
- class_names:
- ?: 1
- lower: 1
- _under: lower 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 code
-
Reposted from Github (PizzaLiu)
PHP, PSR
|