本指南是 PSR-1 基本編碼標準 的擴展。
本指羅列了通用的PHP代碼格式規則和建議,意在減少不同作者的編碼風格差異帶來的認知障礙。
這里的風格約定衍生自若干成員項目。指南作者們在多個項目中協作,推動了這些指導條款落地。 指南的關鍵在于共享,而不是規則本身。
文中涉及的關鍵詞 “MUST 必須”, “MUST NOT 必須不”, “REQUIRED 必需”, “SHALL 會”, “SHALL NOT 不會”, “SHOULD 應該”, “SHOULD NOT 不應該”, “RECOMMENDED 推薦的”, “MAY 可能”, 和 “OPTIONAL 可選的” 在RFC 2119 中有具體描述.
下面是一個綜合的例子,可以幫助你對規則有一個概略的認識。
<?phpnamespace 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($arg1); } else { BazClass::bar($arg2, $arg3); } } final public static function bar() { // 方法 body }}
代碼必須遵循PSR-1的條款。
注意:只使用空格,不用tab,有助于避免diffs,patches, history和annotations的問題。使用空格也有助于對齊。
例如:
<?phpnamespace Vendor/Package;use FooClass;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;// ... additional PHP code ...
這里的“類”包括 class、interface 和 trait。
extends 和 implements 關鍵字必須和類名在同一行聲明。
類的 { 必須獨占一行; } 必須在body的下一行。
<?phpnamespace Vendor/Package;use FooClass;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;class ClassName extends ParentClass implements /ArrayAccess, /Countable{ // constants, properties, methods}
implements 多個接口時,接口列表可以被分到多行,每個子行有一個縮進。如果這么做,第一個接口必須在implements 下一行,每行只能有一個接口。
<?phpnamespace Vendor/Package;use FooClass;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;class ClassName extends ParentClass implements /ArrayAccess, /Countable, /Serializable{ // constants, properties, methods}
所有屬性都必須聲明 visibility(可見性)。
Var 關鍵字必須不能用于聲明屬性。
每行必須只聲明一個屬性。
不應該通過前綴下劃線來標示protected和private的屬性。
例:
<?phpnamespace Vendor/Package;class ClassName{ public $foo = null;}
所有方法都必須聲明可見性。
不應該通過前綴下劃線來標示protected和private的方法。
聲明方法時,方法名的后必須沒有空格。 { 必須在同一行, } 必須在body的下一行。 (后必須沒有空格,) 前必須沒有空格。
一個方法聲明的例子如下,注意 小括號,逗號,空格 和 花括號 的位置:
<?phpnamespace Vendor/Package;class ClassName{ public function fooBarBaz($arg1, &$arg2, $arg3 = []) { // method body }}
方法的形參列表中, 每個逗號前必須沒有空格。有默認值的參數必須在參數列表的最后。
<?phpnamespace Vendor/Package;class ClassName{ public function foo($arg1, &$arg2, $arg3 = []) { // method body }}
參數列表可以切分為多行,每個子行要有一個縮進。如果這么做,第一個參數必須獨占一行,每行只能有一個參數。
參數列表切分為多行時,右括號)和左花括號{必須在同一行,之前必須有一個空格。
<?phpnamespace Vendor/Package;class ClassName{ public function aVeryLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body }}
abstract和final聲明必須在可見性之前聲明。
static聲明必須在可見性之后。
<?phpnamespace Vendor/Package;abstract class ClassName{ protected static $foo; abstract protected function zim(); final public static function bar() { // method body }}
寫方法或函數調用時,方法/函數名 和 左括號( 之間,必須沒有空格, 右括號 ) 之前必須沒有空格。在參數列表中,逗號間必須沒有逗號,每個逗號后必須有一個空格。
<?php$foo->bar( $longArgument, $longerArgument, $muchLongerArgument);
控制結構通常遵循以下風格:
控制結構關鍵詞后必須有一個空格。左括號后必須沒有空格。右括號前必須沒有空格。又括號和左花括號之間必須有一個空格。body必須有一層縮進。右花括號必須在body下一行。每個控制結構的body必須用花括號括起來。 即保證外觀統一,又減少了添加新行時引入的錯誤。if 結構如下所示。注意括號、空格、花括號的位置;同時留意 else 和 elseif 與前一部分的 } 在同一行。
<?phpif ($expr1) { // if body} elseif ($expr2) { // elseif body} else { // else body;}
elseif關鍵字不應該被 else if 代替。
Switch結構如下所示。注意括號、空格和花括號的位置。 case 語句相對于switch必須有一個縮進, break關鍵字 或者其他終結性的關鍵字必須和case body在同一縮進層級。在非空的case body,如果沒有終結性語句,必須加上注釋 // no break。
<?phpswitch ($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;}
while結構如下所示。 注意括號、空格和花括號的位置。
<?phpwhile ($expr) { // structure body}
do-while接口如下所示。 注意括號、空格和花括號的位置。
<?phpdo { // structure body;} while ($expr);
for 結構如下所示。 注意括號、空格和花括號的位置。
<?phpfor ($i = 0; $i < 10; $i++) { // for body}
foreach 結構如下所示。 注意括號、空格和花括號的位置。
<?phpforeach ($iterable as $key => $value) { // foreach body}
try-catch區塊如下所示。 注意括號、空格和花括號的位置。
<?phptry { // try body} catch (FirstExceptionType $e) { // catch body} catch (OtherExceptionType $e) { // catch body}
聲明閉包必須在function關鍵字后留一個空格,在use關鍵字前后各留一個空格。
左花括號必須在同一行, 右花括號必須在body的下一行。
參數或變量列表的左括號后 和 右括號前必須沒有空格。
參數和變量列表的逗號前必須沒有空格,每個逗號后必須有一個空格。
有默認值的參數必須排在最后。
閉包的聲明如下所示。 注意括號,逗號,空格和花括號的位置:
<?php$closureWithArgs = function ($arg1, $arg2) { // body};$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) { // body};
參數列表和變量列表可以拆分到多行,每個子行有一層縮進。 這么做的時候,第一個列表成員必須獨占一行,每行只能有一個列表成員。
參數或變量列表拆分為多行時,到了列表的末尾, 右括號 和 左花括號必須放在同一行,中間有一個空格。
例子:
<?php$longArgs_noVars = function ( $longArgument, $longerArgument, $muchLongerArgument) { // body};$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};
注意:當閉包被直接作為函數或方法調用的參數時,以上規則同樣適用。
<?php$foo->bar( $arg1, function ($arg2) use ($var1) { // body }, $arg3);
本指南刻意忽略了許多風格和實踐。包括但不限于:
聲明全局變量和全局常量。聲明函數。操作符和賦值。行間對齊。注釋和文檔區。類名前后綴。最佳實踐。Future recommendations MAY revise and extend this guide to address those or other elements of style and practice.
In writing this style guide, the group took a survey of member projects to determine common practices. The survey is retained herein for posterity.
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.htmlvoting,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,no,no,no,?,yes,no,yesindent_type,4,4,4,4,4,tab,4,tab,tab,2,4,tab,4,4,4,4,4,4,tab,tab,4,tabline_length_limit_soft,75,75,75,75,no,85,120,120,80,80,80,no,100,80,80,?,?,120,80,120,no,150line_length_limit_hard,85,85,85,85,no,no,no,no,100,?,no,no,no,100,100,?,120,120,no,no,no,noclass_names,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,lower_under,studly,lower,studly,studly,studly,studly,?,studly,studly,studlyclass_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,next,next,next,next,next,next,same,next,nextconstant_names,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,uppertrue_false_null,lower,lower,lower,lower,lower,lower,lower,lower,lower,upper,lower,lower,lower,upper,lower,lower,lower,lower,lower,upper,lower,lowermethod_names,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,lower_under,camel,camel,camel,camel,camel,camel,camel,camel,camel,camelmethod_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,same,next,next,next,next,next,same,next,nextcontrol_brace_line,same,same,same,same,same,same,next,same,same,same,same,next,same,same,next,same,same,same,same,same,same,nextcontrol_space_after,yes,yes,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yesalways_use_control_braces,yes,yes,yes,yes,yes,yes,no,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yeselse_elseif_line,same,same,same,same,same,same,next,same,same,next,same,next,same,next,next,same,same,same,same,same,same,nextcase_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/2function_space_after,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,noclosing_php_tag_required,no,no,no,no,no,no,no,no,yes,no,no,no,no,yes,no,no,no,no,no,yes,no,noline_endings,LF,LF,LF,LF,LF,LF,LF,LF,?,LF,?,LF,LF,LF,LF,?,,LF,?,LF,LF,LFstatic_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,noblank_line_after_php,no,no,no,no,yes,no,no,no,no,yes,yes,no,no,yes,?,yes,yes,no,yes,no,yes,noclass_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
indent_type: The type of indenting. tab = “Use a tab”, 2 or 4 = “number of spaces”
line_length_limit_soft: The “soft” line length limit, in characters. ? = not discernible or no response, no means no limit.
line_length_limit_hard: The “hard” line length limit, in characters. ? = not discernible or no response, no means no limit.
class_names: How classes are named. lower = lowercase only, lower_under = lowercase with underscore separators, studly = StudlyCase.
class_brace_line: Does the opening brace for a class go on the same line as the class keyword, or on the next line after it?
constant_names: How are class constants named? upper = Uppercase with underscore separators.
true_false_null: Are the true, false, and null keywords spelled as all lower case, or all upper case?
method_names: How are methods named? camel = camelCase, lower_under = lowercase with underscore separators.
method_brace_line: Does the opening brace for a method go on the same line as the method name, or on the next line?
control_brace_line: Does the opening brace for a control structure go on the same line, or on the next line?
control_space_after: Is there a space after the control structure keyword?
always_use_control_braces: Do control structures always use braces?
else_elseif_line: When using else or elseif, does it go on the same line as the previous closing brace, or does it go on the next line?
case_break_indent_from_switch: How many times are case and break indented from an opening switch statement?
function_space_after: Do function calls have a space after the function name and before the opening parenthesis?
closing_php_tag_required: In files containing only PHP, is the closing ?> tag required?
line_endings: What type of line ending is used?
static_or_visibility_first: When declaring a method, does static come first, or does the visibility come first?
control_space_parens: In a control structure expression, is there a space after the opening parenthesis and a space before the closing parenthesis? yes = if (
blank_line_after_php: Is there a blank line after the opening PHP tag?
class_method_control_brace: A summary of what line the opening braces go on for classes, methods, and control structures.
indent_type: tab: 7 2: 1 4: 14line_length_limit_soft: ?: 2 no: 3 75: 4 80: 6 85: 1 100: 1 120: 4 150: 1line_length_limit_hard: ?: 2 no: 11 85: 4 100: 3 120: 2class_names: ?: 1 lower: 1 lower_under: 1 studly: 19class_brace_line: next: 16 same: 6constant_names: upper: 22true_false_null: lower: 19 upper: 3method_names: camel: 21 lower_under: 1method_brace_line: next: 15 same: 7control_brace_line: next: 4 same: 18control_space_after: no: 2 yes: 20always_use_control_braces: no: 3 yes: 19else_elseif_line: next: 6 same: 16case_break_indent_from_switch: 0/1: 4 1/1: 4 1/2: 14function_space_after: no: 22closing_php_tag_required: no: 19 yes: 3line_endings: ?: 5 LF: 17static_or_visibility_first: ?: 5 either: 7 static: 4 visibility: 6control_space_parens: ?: 1 no: 19 yes: 2blank_line_after_php: ?: 1 no: 13 yes: 8class_method_control_brace: next/next/next: 4 next/next/same: 11 next/same/same: 1 same/same/same: 6
PHP編程 鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答