本篇规范是 [PSR-1][] 基本代码规范的继承与扩展。php
本规范但愿经过制定一系列规范化PHP代码的规则,以减小在浏览不一样做者的代码时,因代码风格的不一样而形成不便。html
当多名程序员在多个项目中合做时,就须要一个共同的编码规范,
而本文中的风格规范源自于多个不一样项目代码风格的共同特性,
所以,本规范的价值在于咱们都遵循这个编码风格,而不是在于它自己。git
关键词 “必须”("MUST")、“必定不可/必定不能”("MUST NOT")、“须要”("REQUIRED")、
“将会”("SHALL")、“不会”("SHALL NOT")、“应该”("SHOULD")、“不应”("SHOULD NOT")、
“推荐”("RECOMMENDED")、“能够”("MAY")和”可选“("OPTIONAL")的详细描述可参见 [RFC 2119][] 。程序员
概览github
代码必须遵循 [PSR-1][] 中的编码规范 。闭包
代码必须使用4个空格符而不是 tab键 进行缩进。app
每行的字符数应该软性保持在80个以内, 理论上必定不可多于120个, 但必定不能有硬性限制。编辑器
每一个 namespace
命名空间声明语句和 use
声明语句块后面,必须插入一个空白行。ide
类的开始花括号({
)必须写在函数声明后自成一行,结束花括号(}
)也必须写在函数主体后自成一行。函数
方法的开始花括号({
)必须写在函数声明后自成一行,结束花括号(}
)也必须写在函数主体后自成一行。
类的属性和方法必须添加访问修饰符(private
、protected
以及 public
), abstract
以及 final
必须声明在访问修饰符以前,而 static
必须声明在访问修饰符以后。
控制结构的关键字后必须要有一个空格符,而调用方法或函数时则必定不能有。
控制结构的开始花括号({
)必须写在声明的同一行,而结束花括号(}
)必须写在主体后自成一行。
控制结构的开始左括号后和结束右括号前,都必定不能有空格符。
如下例子程序简单地展现了以上大部分规范:
<?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($arg1); } else { BazClass::bar($arg2, $arg3); } } final public static function bar() { // method body } }
通则
代码必须符合 [PSR-1][] 中的全部规范。
全部PHP文件必须使用Unix LF (linefeed)
做为行的结束符。
全部PHP文件必须以一个空白行做为结束。
纯PHP代码文件必须省略最后的 ?>
结束标签。
行的长度必定不能有硬性的约束。
软性的长度约束必定要限制在120个字符之内,若超过此长度,带代码规范检查的编辑器必定要发出警告,不过必定不可发出错误提示。
每行不该该多于80个字符,大于80字符的行应该折成多行。
非空行后必定不能有多余的空格符。
空行能够使得阅读代码更加方便以及有助于代码的分块。
每行必定不能存在多于一条语句。
代码必须使用4个空格符的缩进,必定不能用 tab键 。
备注: 使用空格而不是tab键缩进的好处在于,
避免在比较代码差别、打补丁、重阅代码以及注释时产生混淆。
而且,使用空格缩进,让对齐变得更方便。
PHP全部 [关键字][]必须所有小写。
常量 true
、false
和 null
也必须所有小写。
namespace 以及 use 声明
namespace
声明后 必须 插入一个空白行。
全部 use
必须 在 namespace
后声明。
每条 use
声明语句 必须 只有一个 use
关键词。
use
声明语句块后 必须 要有一个空白行。
例如:
<?php namespace Vendor\Package; use FooClass; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; // ... additional PHP code ...
类、属性和方法
此处的“类”泛指全部的class类、接口以及traits可复用代码块。
关键词 extends
和 implements
必须写在类名称的同一行。
类的开始花括号必须独占一行,结束花括号也必须在类主体后独占一行。
<?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
的继承列表也能够分红多行,这样的话,每一个继承接口名称都必须分开独立成行,包括第一个。
<?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 }
每一个属性都必须添加访问修饰符。
必定不可使用关键字 var
声明一个属性。
每条语句必定不可定义超过一个属性。
不要使用下划线做为前缀,来区分属性是 protected 或 private。
如下是属性声明的一个范例:
<?php namespace Vendor\Package; class ClassName { public $foo = null; }
全部方法都必须添加访问修饰符。
不要使用下划线做为前缀,来区分方法是 protected 或 private。
方法名称后必定不能有空格符,其开始花括号必须独占一行,结束花括号也必须在方法主体后单独成一行。参数左括号后和右括号前必定不能有空格。
一个标准的方法声明可参照如下范例,留意其括号、逗号、空格以及花括号的位置。
<?php namespace Vendor\Package; class ClassName { public function fooBarBaz($arg1, &$arg2, $arg3 = []) { // method body } }
参数列表中,每一个逗号后面必须要有一个空格,而逗号前面必定不能有空格。
有默认值的参数,必须放到参数列表的末尾。
<?php namespace Vendor\Package; class ClassName { public function foo($arg1, &$arg2, $arg3 = []) { // method body } }
参数列表能够分列成多行,这样,包括第一个参数在内的每一个参数都必须单独成行。
拆分红多行的参数列表后,结束括号以及方法开始花括号 必须 写在同一行,中间用一个空格分隔。
<?php namespace Vendor\Package; class ClassName { public function aVeryLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body } }
abstract
、 final
、 以及 static
须要添加 abstract
或 final
声明时, 必须写在访问修饰符前,而 static
则必须写在其后。
<?php namespace Vendor\Package; abstract class ClassName { protected static $foo; abstract protected function zim(); final public static function bar() { // method body } }
方法及函数调用时,方法名或函数名与参数左括号之间必定不能有空格,参数右括号前也 必定不能有空格。每一个参数前必定不能有空格,但其后必须有一个空格。
<?php bar(); $foo->bar($arg1); Foo::bar($arg2, $arg3);
参数能够分列成多行,此时包括第一个参数在内的每一个参数都必须单独成行。
<?php $foo->bar( $longArgument, $longerArgument, $muchLongerArgument );
控制结构
控制结构的基本规范以下:
控制结构关键词后必须有一个空格。
左括号 (
后必定不能有空格。
右括号 )
前也必定不能有空格。
右括号 )
与开始花括号 {
间必定有一个空格。
结构体主体必定要有一次缩进。
结束花括号 }
必定在结构体主体后单独成行。
每一个结构体的主体都必须被包含在成对的花括号之中,
这能让结构体更加结构话,以及减小加入新行时,出错的可能性。
if
、 elseif
和 else
标准的 if
结构以下代码所示,留意 括号、空格以及花括号的位置,
注意 else
和 elseif
都与前面的结束花括号在同一行。
<?php if ($expr1) { // if body } elseif ($expr2) { // elseif body } else { // else body; }
应该使用关键词 elseif
代替全部 else if
,以使得全部的控制关键字都像是单独的一个词。
switch
和 case
标准的 switch
结构以下代码所示,留意括号、空格以及花括号的位置。case
语句必须相对 switch
进行一次缩进,而 break
语句以及 case
内的其它语句都 必须 相对 case
进行一次缩进。
若是存在非空的 case
直穿语句,主体里必须有相似 // no break
的注释。
<?php 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; }
while
和 do while
一个规范的 while
语句应该以下所示,注意其 括号、空格以及花括号的位置。
<?php while ($expr) { // structure body }
标准的 do while
语句以下所示,一样的,注意其 括号、空格以及花括号的位置。
<?php do { // structure body; } while ($expr);
for
标准的 for
语句以下所示,注意其 括号、空格以及花括号的位置。
<?php for ($i = 0; $i < 10; $i++) { // for body }
foreach
标准的 foreach
语句以下所示,注意其 括号、空格以及花括号的位置。
<?php foreach ($iterable as $key => $value) { // foreach body }
try
, catch
标准的 try catch
语句以下所示,注意其 括号、空格以及花括号的位置。
<?php try { // try body } catch (FirstExceptionType $e) { // catch body } catch (OtherExceptionType $e) { // catch body }
闭包
闭包声明时,关键词 function
后以及关键词 use
的先后都必须要有一个空格。
开始花括号必须写在声明的同一行,结束花括号必须紧跟主体结束的下一行。
参数列表和变量列表的左括号后以及右括号前,必须不能有空格。
参数和变量列表中,逗号前必须不能有空格,而逗号后必须要有空格。
闭包中有默认值的参数必须放到列表的后面。
标准的闭包声明语句以下所示,注意其 括号、逗号、空格以及花括号的位置。
<?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 );
总结
以上规范不免有疏忽,其中包括但不只限于:
全局变量和常量的定义
函数的定义
操做符和赋值
行内对齐
注释和文档描述块
类名的前缀及后缀
最佳实践
本规范以后的修订与扩展将弥补以上不足。
为了编写本规范,小组制定了调查问卷,用来统计各成员项目的共同规范。
如下是此问卷调查的数据,在此供查阅。
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
indent_type
:
缩进类型. tab
= "使用 tab 键一次", 2
or 4
= "空格的数量"
line_length_limit_soft
:
每行字符数量的“软”限制. ?
= 不可辩或无做答, no
表示无限制.
line_length_limit_hard
:
每行字符数量的“硬”限制. ?
= 不可辩或无做答, no
表示无限制.
class_names
:
类名称的命名. lower
= 只容许小写字母, lower_under
= 下滑线分隔的小写字母, studly
= StudlyCase 的驼峰风格.
class_brace_line
:
类的开始花括号是与 class 关键字在同一行或是在其的下一行?
constant_names
:
类的常量如何命名? upper
= 下划线分隔的大写字母.
true_false_null
:
关键字 true
、false
以及 null
是所有小写 lower
仍是所有大写 upper
?
method_names
:
方法名称如何命名? camel
= camelCase
, lower_under
= 下划线分隔的小写字母.
method_brace_line
:
方法的开始花括号是与方法名在同一行仍是在其的下一行?
control_brace_line
:
控制结构的开始花括号是与声明在同一行仍是在其的下一行?
control_space_after
:
控制结构关键词后是否有空格?
always_use_control_braces
:
控制结构体是否都要被包含在花括号内?
else_elseif_line
:else
或 elseif
与前面的结束花括号在同一行仍是在其的下一行?
case_break_indent_from_switch
:switch
语句中的 case
和 break
须要相对 switch
缩进多少次?
function_space_after
:
函数调用语句中,函数名称与变量列表的左括号间是否有空格?
closing_php_tag_required
:
纯 PHP 代码的文件,是否须要 ?>
结束标签?
line_endings
:
选择哪一种类型的行结束符?
static_or_visibility_first
:
声明一个静态方法时,static
是写访问修饰符前仍是后?
control_space_parens
:
控制结构里,左括号后以及右括号前是否有空格?yes
= if ( $expr )
, no
= if ($expr)
.
blank_line_after_php
:
PHP 开始标签后,是否须要一个空行?
class_method_control_brace
:
开始花括号在类、方法和控制结构的位置统计。
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