Document warehouse address:https://github.com/hfcorriez/fig-standards
Chinese version of PSR specification
1.1. Example
This example contains a simple display of some of the above rules:
class Foo extends Bar implements FooInterface
{
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
}
}
2. Summary
2.1 Basic Code Specifications
Code must comply with all rules of PSR-1.
2.2 Files
All PHP files must use Unix LF (line feed) as the line terminator.
All PHP files must end with a blank line.
File closing tag for pure PHP code?> must be omitted
2.3. Lines
There cannot be a hard limit on line length.
The soft limit on line length must be 120 characters; the automatic style checker must warn but not error about the soft limit.
The actual length of aline should not exceed 80 characters; longer lines should be split into subsequent lines of no more than 80 characters.
There must be no spaces after non-blank lines.
Blank lines can be used to improve readability and distinguish related blocks of code.
There should be no more than one statement per line.
2.4. Indentation
Code must use 4 spaces for indentation, and tab characters cannot be used as indentation.
Note: Using only spaces, not mixed with tabs, will help avoid some problems in code differences, patches, history and comments. Using whitespace also makes it very easy to adjust subtle indentations to improve alignment between lines.
2.5. Keywords and True/False/Null
PHP keywords must be lowercase.
PHP constants true, false and null must be lowercase.
3. Namespace and Use declarations
If present, there must be a blank line after the namespace declaration.
If present, all use statements must be placed below the namespace statement.
A use keyword must be used in only one declaration.
There must be a blank line after the use declaration block.
Example:
use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
// ... additional PHP code ...
4. Classes, Properties and Methods
The term "class" refers to all classes, interfaces and characteristics (traits).
4.1. Extension and inheritance
The extends and implements keywords of a class must be on the same line as the class name.
The left curly brace of the class must be placed on its own line below; the right curly brace must be placed on its own line after the class body.
class ClassName extends ParentClass implements \ArrayAccess, \Countable
{
// constants, properties, methods
}
implementsA list can be split into multiple subsequent lines with one indentation. If you do this, the first item in the list must be placed on the next line, and there must be only one interface per line.
use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class ClassName extends ParentClass implements
\ArrayAccess,
\Countable,
\Serializable
{
// constants, properties, methods
}
4.2. Properties
All properties must declare visibility.
The var keyword cannot be used to declare attributes.
One statement cannot declare multiple attributes.
Attribute names should not be prefixed with a single underscore to indicate protected or private visibility.
A property declaration should look like this.
class ClassName
{
public $foo = null;
}
4.3. Methods
All methods must declare visibility.
Method names should not use only a single underscore to indicate protected or private visibility.
The method name cannot be followed by a space after the declaration. The opening curly brace must be placed on its own line below, and the closing curly brace must be placed on its own line below the method body. There must be no spaces after the left bracket and no spaces before the right bracket.
A method definition should look like the following. Note the parentheses, commas, spaces and curly braces:
class ClassName
{
public function fooBarBaz($arg1, &$arg2, $arg3 = [])
{
// method body
}
}
4.4. Method parameters
In the parameter list, there must be no space before the comma, and there must be one space after the comma.
Parameters with default values in the method must be placed at the end of the parameter list.
class ClassName
{
public function foo($arg1, &$arg2, $arg3 = [])
{
// method body
}
}
The parameter list can be divided into multiple subsequent lines with one indentation. If you do this, the first item in the list must be placed on the next line, and only one parameter must be placed on each line.
When the parameter list is divided into multiple lines, the right bracket and the left curly bracket must be placed on a single line with a space between them.
class ClassName
{
public function aVeryLongMethodName(
ClassTypeHint $arg1,
&$arg2,
array $arg3 = []
) {
// method body
}
}
4.5. abstract, final and static
If present, abstract and final declarations must be placed before the visibility declaration.
If present, a static declaration must be followed by a visibility declaration.
abstract class ClassName
{
protected static $foo;
abstract protected function zim();
final public static function bar()
{
// method body
}
}
4.6. Calling methods and functions
To call a method or function, there must be no spaces between the method or function name and the left bracket, no spaces after the left bracket, and no spaces before the right bracket. In the function list, there must be no space before the comma and there must be one space after the comma.
bar();
$foo->bar($arg1);
Foo::bar($arg2, $arg3);
The parameter list can be split into Multiple following lines have an indent. If you do this, the first item in the list must be placed on the next line, and each line must have exactly one argument.
5. Control structure
The style rules for control structures are summarized 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 left curly bracket
Code The body must be indented once
The closing curly brace must be one line below the body
The body of each structure must be enclosed in curly braces. This structure looks more standardized and reduces the possibility of introducing errors when adding new lines.
5.1. if, elseif, else
An if structure should look like the following. Pay attention to the placement of parentheses, spaces, and curly braces; and else and elseif are on the same line as the closing curly brace of the previous body.
The keyword elseif should be used instead of else if to keep all control keywords as one word.
5.2. switch, case
A switch structure should look like the following. Pay attention to the parentheses, spaces and curly braces. The case statement must be indented from the switch, and the break keyword (or other break keyword) must be indented at the same level as the case body. If a non-empty case body falls down, there must be a comment like // no break.
5.4. for
A for statement should look like the following. Pay attention to the placement of parentheses, spaces, and curly braces.
5.5. foreach
A foreach statement should look like the following. Pay attention to the placement of parentheses, spaces, and curly braces.
6. Closure
A closure must have a space after the function keyword when declared, and a space before use.
The opening curly brace must be on the same line, and the closing curly brace must be on the next line after the body.
There must be no spaces after the opening brackets of parameter lists and variable lists, and there must be no spaces before the closing brackets.
In parameter lists and variable lists, there must be no spaces before the comma and there must be spaces after the comma.
Parameters of a closure with default values must be placed after the parameter list.
A closure declaration should look like the following. Pay attention to the placement of parentheses, spaces, and curly braces.
Parameter and variable lists can be split into multiple subsequent lines with one indentation. If you do this, the first item in the list must be placed on the next line, and only one parameter or variable must be placed on a line.
When the final list (whether parameters or variables) is divided into multiple lines, the closing bracket and the opening curly bracket must be placed on their own line with a space between them.
The following is an example of a parameter and variable list split into multiple lines.
$noArgs_longVars = function () use (
$longVar1,
$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
};
Note that if the closure is called as a parameter in a function or method, the above formatting rules also apply.
7. Conclusion
There are many stylistic elements and practices that have been intentionally left out of this guide. These include but are not limited to:
Declaration of global variables and global constants
Method declaration
Operators and assignment
Alignment between lines
Comments and documentation blocks
The class name gives you the prefix and suffix
Best Practices
Future proposals may modify and expand this guide to meet these or other style elements and practices.
Appendix A Survey
To write this style guide, we used a survey of projects to identify common practices. The survey is here for others to view.
A.1. 调查数据
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:-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
A.2. 调查说明
indent_type: 缩进类型。tab = "Use tab character", 2 or 4 = "Number of spaces"
line_length_limit_soft: "soft" limit on line length, in characters. ? = does not mean no or the number no means no limit.
line_length_limit_hard: "hard" limit on line length, in characters. ? = does not mean or number, no means no limit.
class_names: How to name class names lower = just lowercase, lower_under = lowercase plus underline, studly = camel type.
class_brace_line: Should the left brace of a class be placed on the same line or on the next line?
constant_names: How to name class constants? upper = uppercase plus underline delimiter.
true_false_null: All letters or all capital letters?
method_names: How to name method names? camel = camel case, lower_under = lowercase plus underline delimiter.
method_brace_line: Is the opening brace of the method on the same line or on the next line?
control_brace_line: Is the left brace of the control structure on the same line or the next line?
control_space_after: Is there a space after the control structure keyword?
always_use_control_braces: Always use braces for control structures?
else_elseif_line: When using else and elseif, should they be placed on the same line or on the next line?
case_break_indent_from_switch: How many times are case and break indented from the switch statement?
function_space_after: Are there spaces in the function name and left bracket of the function call?
closing_php_tag_required: If it is a pure PHP file, is closing the tag?> required?
line_endings: What line endings are used?
static_or_visibility_first: When defining a method, which one comes first, static or visibility?
control_space_parens: In the control structure expression, is there a space after the left bracket and before the right bracket? yes = if ( $expr ), no =if ($expr).
blank_line_after_php: Is a blank line required after the PHP start tag?
class_method_control_brace: The position of the left curly brace in classes, methods and control structures.
A.3. 调查结果
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