Fig organization in the development of PHP-related specifications, referred to as PSR. There are now 4 code specifications, the recent time to translate into the Chinese version. Suggest doing PHP students are concerned about.
Document Warehouse Address: Https://github.com/hfcorriez/fig-standards
All accepted specification references: https://github.com/hfcorriez/fig-standards/tree/zh_CN/%E6%8E%A5%E5%8F%97
Code Style Specification
The intent of this guide is to reduce the cognitive differences between different developers when browsing the code. This lists a set of common rules for how to format PHP code.
The generality of each member's project constitutes the style rule of this article. When different developers collaborate on different projects, a common standard will be used in these different projects. Therefore, the benefits of this guide lie not in the rules themselves, but in the sharing of these rules.
Feature keywords in RFC 2119 "must" (must), "must" (not), "necessary" (REQUIRED), "will" (SHALL), "No" (SHALL not), "should" (SHOULD), "No" (SHOULD not) , "recommended" (recommended), "may" and "optional" (OPTIONAL) will be used in this document for description.
1. Outline
Code must comply with PSR-1.
The code must use an indentation of 4 spaces instead of a tab character.
The length of a line of code should not be rigidly limited, the soft limit must be 120 characters, or it should be 80 characters or less.
There must be a blank line under the namespace declaration, and a blank line must be below the use declaration code block.
The left curly brace for a class must be placed on the next line, and the right curly brace must be placed on the next line of the class body.
The left curly brace for the method must be placed on the next line, and the right curly brace must be placed below the method body.
All attributes and methods must have visibility (translator note: Public, Protect, Private), abstract and final declaration must precede visibility, static declaration must be after visibility.
The keywords in the control structure must be followed by a space, and methods and functions are not available.
The left curly braces for the control structure must be placed on the same line, and the right curly braces must be placed on the next line of the control body.
There are no spaces behind the left parenthesis of the control structure, and no spaces before the closing parenthesis.
1.1. Example
This example includes some of the above rules for a simple demonstration:
<?php
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
}
}
2. Summary
2.1 Basic Code specification
The code must comply with all PSR-1 rules.
2.2 Files
All PHP files must use UNIX LF (newline) as the line terminator.
All PHP files must end with a blank line.
Pure PHP code for file close tag?> must be omitted
2.3. Line
There is no hard limit on the length of the line.
The soft limit for the length of the line must be 120 characters; for soft limits, the automatic style checker must warn but not to complain.
The actual length of a row should not exceed 80 characters, and a longer row should be split into several subsequent rows that do not exceed 80 characters.
There is no space after a non-empty line.
Empty lines can be used to improve readability and distinguish between related blocks of code.
A line should not have more than one statement.
2.4. Indent
The code must be indented with 4 spaces, and tabs cannot be used as indents.
Note: Using only spaces, not with tabs, will be helpful in avoiding code differences, patches, histories, and annotations. Using spaces also makes it easy to adjust the subtle indentation to improve alignment between rows.
2.5. Keywords and true/false/null
PHP keywords must use lowercase.
PHP constants True, FALSE, and null must use lowercase.
3. Namespace and use statement
If present, there must be a blank line after the namespace declaration.
If present, all use declarations must be placed below the namespace declaration.
A use keyword must be used only for one declaration.
There must be a blank line after the use declaration code block.
Example:
<?php
namespace Vendor\package;
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 attributes (traits).
4.1. Extension and inheritance
The extends and implements keywords for a class must be on the same line as the class name.
The left curly brace of a class must be placed below its own line, and the right curly braces must be placed behind the class body in its own line.
<?php
namespace Vendor\package;
Use Fooclass;
Use Barclass as Bar;
Use Othervendor\otherpackage\bazclass;
Class ClassName extends ParentClass implements \arrayaccess, \countable
{
//constants, properties, methods
}
Implements a list can be split into multiple subsequent rows that have one indent at a time. If you do this, the first item in the list must be placed on the next line, and each row must have only one interface.
<?php
namespace Vendor\package;
Use Fooclass;
Use Barclass as Bar;
Use Othervendor\otherpackage\bazclass;
Class ClassName extends ParentClass implements
\arrayaccess, \countable
, \serializable
{
Constants, properties, Methods
}
4.2. Property
All attributes must declare visibility.
var keywords cannot be used to declare properties.
A statement cannot declare multiple properties.
Property names should not use a single underline as a prefix to indicate protection or private visibility.
A property declaration should look like the following.
<?php
namespace Vendor\package;
Class ClassName
{public
$foo = null;
}
4.3. Methods
All methods must declare visibility.
Method names should not use only a single underline to indicate a protected or private visibility.
The method name cannot follow a space after the declaration. The left curly brace must be placed below its own line, and the right curly brace must be placed underneath the method body in its own line. There are no spaces behind the opening parenthesis, no spaces in front of the closing parenthesis.
A method definition should look something like the following. Note parentheses, commas, spaces, and braces:
<?php
namespace Vendor\package;
Class ClassName
{public
function Foobarbaz ($arg 1, & $arg 2, $arg 3 = [])
{
//
method body}< c15/>}
4.4. Method parameters
In the argument list, there must be no space before the comma, and a space after the comma.
A parameter with a default value in the method must be placed at the end of the argument list.
<?php
namespace Vendor\package;
Class ClassName
{public
function foo ($arg 1, & $arg 2, $arg 3 = [])
{
//mode body
}
}
A parameter list can be divided into multiple subsequent rows that have one indent at a time. If you do this, the first item in the list must be placed on the next line, and each row must have only one argument.
When the argument list is divided into multiple lines, the closing parenthesis and the left curly brace must be packed together into one line.
<?php
namespace Vendor\package;
Class ClassName
{public
function averylongmethodname (
classtypehint $arg 1,
& $arg 2,
array $ Arg3 = []
) {
//method body
}
}
4.5. Abstract,final and Static
If present, the abstract and final declaration must precede the visibility declaration.
If present, the static declaration must follow the visibility declaration.
<?php
namespace Vendor\package;
Abstract class ClassName
{
protected static $foo;
Abstract protected function Zim ();
Final public static function bar ()
{
//method body
}
}
4.6. Calling methods and functions
To invoke a method or function, there must be no space between the method or the opening parenthesis, and no spaces after the opening parenthesis, and no spaces before the closing parenthesis. In the list of functions, there should be no spaces before commas, and a comma must be followed by a space.
<?php
Bar ();
$foo->bar ($arg 1);
Foo::bar ($arg 2, $arg 3);
A parameter list can be split into multiple subsequent rows that have one indentation. If you do this, the first item in the list must be placed on the next line, and each row must have only one argument.
<?php
$foo->bar (
$longArgument,
$longerArgument,
$muchLongerArgument
);
5. Control Structure
The style rules for the control structure are summarized as follows:
- You must have a space after you control the structure keyword
- No spaces after opening parenthesis
- No spaces before closing parenthesis
- There must be a space between the closing parenthesis and the left curly brace
- The code body must have one indent
- Right curly braces must be the next line of the body
The body of each structure must be enclosed in curly braces. This structure looks more standardized and can reduce the likelihood of introducing errors when Ghanio.
5.1. If,elseif,else
An If structure should look like the following. Note the parentheses, spaces, and the position of the curly braces, and the else and ElseIf and the right curly braces of the previous body are on the same line.
<?php
if ($expr 1) {
//if Body
} elseif ($expr 2) {
//ElseIf body
} else {
/else body;< c16/>}
Keyword ElseIf should replace else if used to keep all the control keywords like a word.
5.2. Switch,case
A switch structure should look something like the following. Pay attention to parentheses, spaces, and braces. The case statement must be indented from the switch, and the break keyword (or other abort keyword) must be indented with the case body at the same sibling. If a non-empty case body is dashed down, there must be a comment similar to/no break.
<?php
Switch ($expr) {case
0:
echo ' in the ', with a-break ';
break;
Case 1:
Echo ' Second case, which falls through ';
No.
2: Case
3: Case
4:
Echo ' third case, return instead of break ';
return;
Default:
echo ' default case ';
break;
5.3. While,do while
A while statement should look like the following. Note the brackets, spaces, and braces positions.
<?php while
($expr) {
//structure body
}
Similarly, a do-while statement should look something like the following. Note the brackets, spaces, and braces positions.
<?php do
{
//structure body; while
($expr);
5.4. For
A For statement should look like the following. Note the brackets, spaces, and braces positions.
<?php for
($i = 0; $i < $i + +) {
//for Body
}
5.5. foreach
A foreach statement should look something like the following. Note the brackets, spaces, and braces positions.
<?php
foreach ($iterable as $key => $value) {
//foreach Body
}
5.6. Try, catch
A try catch statement should look like the following. Note the brackets, spaces, and braces positions.
<?php
try {
//try Body
} catch (Firstexceptiontype $e) {
//catch Body
} catch (Otherexceptionty PE $e) {
//catch Body
}
6. Closure of the package
Closures must have a space after the function keyword is declared, and a space before use.
The left curly braces must be on the same line, and the right curly braces must be on the next line of the body.
After the left parenthesis of the argument list and the variable list, there should be no spaces, and no spaces before the closing parenthesis.
In the argument list and the list of variables, there must be no spaces before the comma, followed by a space.
Parameters with default values for closures must be placed behind the argument list.
A closure statement should look something like the following. Note the brackets, spaces, and braces positions.
<?php
$closureWithArgs = function ($arg 1, $arg 2) {
//Body
};
$closureWithArgsAndVars = function ($arg 1, $arg 2) use ($var 1, $var 2) {
//body
};
A list of parameters and variables can be divided into successive rows with one indent at a time. If you do this, the first item in the list must be placed on the next line, and a row must have only one argument or variable.
When the final list (whether a parameter or a variable) is divided into multiple lines, the closing parenthesis and the left curly brace must be packed together into one line.
The following is an example of a parameter and a list of variables that are split into multiple rows.
<?php
$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 ($ VAR1) {
//Body
};
$shortArgs _longvars = function ($arg) use (
$longVar 1,
$longerVar 2,
$muchLongerVar 3
) {
// Body
};
Note If the closure is invoked as a parameter in a function or method, the same applies to the formatting rules above.
<?php
$foo->bar (
$arg 1,
function ($arg 2) use ($var 1) {
//Body
},
$arg 3
);
7. Conclusion
There are many styles of elements and practices that are intentionally overlooked in the guide. These include but are not limited to:
Declarations of global variables and global constants
Method declaration
Operators and Assignments
Align between rows
Comments and Document blocks
Class name gives you the prefix and suffix
Best practices
Future recommendations can modify and extend the guide to meet these or other stylistic elements and practices.
Appendix A survey
To write this style guide, we used a survey project to identify common practices. This survey is here for others to see.
A.1. Survey 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
A.2. Survey Notes
Indent_type: Indentation type. tab = "Use tabs", 2 or 4 = "Number of spaces"
Line_length_limit_soft: The length of the "soft" limit, with the character.? = does not mean or the number no means unlimited.
Line_length_limit_hard: The length of the "hard" limit, with the character.? = no representation or number, no means no limit.
Class_names: How the class name is named lower = only lowercase, Lower_under = lowercase underlined, studly = Camel type.
Class_brace_line: Does the left curly brace for the class be placed on the same (same) line or down (next) line?
Constant_names: How is the class constant named? Upper = uppercase plus underscore separator.
True_false_null: whole school or all caps?
Method_names: How is the method name named? Camel = hump, Lower_under = lowercase plus underscore separator.
Method_brace_line: Is the left curly brace of the method on the same (same) line or down (next) line?
Control_brace_line: Is the left curly brace of the control structure on the same (same) line or down (next) line?
Control_space_after: Is there any space after controlling the structure keyword?
Always_use_control_braces: Control structure always uses curly braces?
Else_elseif_line: When you use else and ElseIf, do you want to put it on the same (same) line or down (next) line?
How many times case_break_indent_from_switch:case and break are indented from the Swith statement?
Function_space_after: Does the function call have spaces in the function name and the opening parenthesis?
Closing_php_tag_required: If it is a pure php file, is it necessary to turn off the label?>?
Line_endings: What line terminator is used?
Static_or_visibility_first: Static and visibility when defining a method who's in the front?
Control_space_parens: In a control structure expression, is there a space before the left parenthesis and the closing parenthesis? Yes = if ($expr), no =if ($expr).
Do you need a blank line after the blank_line_after_php:php start tag?
Class_method_control_brace: The position of the left curly braces in the class, method, and control structure.
A.3. Results of the survey
Indent_type:tab:7 2:1 4:14 line_length_limit_soft:?: 2 no:3 75:4 80:6 85:1 1 00: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:2 2 true_false_null:lower:19 upper:3 method_names:camel:21 lower_under:1 method_brace_line:next: 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: 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: One next/same/same:1 Same/same/same:6