Home  >  Article  >  Backend Development  >  Detailed introduction to PHP's PSR-2 coding style specification (code)

Detailed introduction to PHP's PSR-2 coding style specification (code)

不言
不言forward
2019-04-01 10:15:101777browse

This article brings you a detailed introduction (code) about PHP's PSR-2 coding style specification. It has certain reference value. Friends in need can refer to it. I hope It will help you.

Next is the PSR-2 coding style specification, which is the inheritance and extension of the PSR-1 basic code specification.

PSR-1 and PSR-2 are the basic coding standards in PHP development. In fact, everyone can refer to and learn from them. Although every developer has his own set of development standards that he is familiar with, I think it is still Write our code according to industry standards. After all, the standards have been verified. I hope it will be helpful to PHP developers.

1. Overview

The code must follow the coding specifications in [PSR-1]().

Code must be indented using 4 spaces instead of the "Tab key".

The number of characters per line should be softly kept within 80. In theory, it must not be more than 120, but there must not be a hard limit.

A blank line must be inserted after each namespace declaration statement and use declaration block.

The opening brace ({) of a class must be written on its own line after the class declaration, and the closing brace (}) must also be written on its own line after the class body.

The opening curly brace ({) of the 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.

The properties and methods of the class must add access modifiers (private, protected and public), abstract and final must be declared before the access modifier, and static must be declared after the access modifier.

There must be a space after the keyword of the control structure, but it must not be when calling a method or function.

The opening brace ({) of the control structure must be written on the same line as the declaration, and the closing 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:

<?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()
 {
 // 方法的内容
 }
}

2. General rules

2.1 Basic Coding Guidelines

Code must comply with all specifications in [PSR-1]().

2.2 Files

All PHP files must use Unix LF (linefeed) as the line terminator.

All PHP files must end with a blank line.

Pure PHP code files must omit the final ?> closing tag.

2.3. The length of the line

must not have a hard limit.

Soft length constraints must be limited to 120 characters. If this length is exceeded, the editor with code specification checking must issue a warning, but must not issue an error prompt.

Each line should not exceed 80 characters, and lines longer than 80 characters should be folded into multiple lines.

There must be no extra spaces after a non-blank line.

Blank lines can make it easier to read the code and help block the code.

Each line must not contain more than one statement.

2.4. Indentation

Code must use 4 spaces for indentation, and the tab key must not be used.

Note: The advantage of using spaces instead of "tab indentation" is that

avoids confusion when comparing code differences, patching, rereading code, and comments.

And, use spaces for indentation to make alignment more convenient.

2.5. Keywords and True/False/Null

All PHP keywords must be all lowercase.

The constants true, false and null must also be all lowercase.

3. Namespace and use declarations

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.

use There must be a blank line after declaring the statement block.

For example:

<?php

namespace Vendor\Package;
use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

4. Classes, properties and methods

The "class" here refers to all "class classes", " Interfaces" and "traits reusable code blocks".

4.1. Extension and inheritance

The keywords extends and implements must be written on the same line of the class name.

The opening curly brace of a class must occupy its own line, and the closing curly brace must also occupy its own line after the class body. The inheritance list for

<?php
namespace Vendor\Package;
use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class ClassName extends ParentClass implements \ArrayAccess, \Countable
{
 // 这里面是常量、属性、类方法
}

implements can also be split into multiple lines, in which case each inherited interface name must be on its own line, including the first one.

<?php

namespace Vendor\Package;
use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class ClassName extends ParentClass implements
 \ArrayAccess,
 \Countable,
 \Serializable
{
 // 这里面是常量、属性、类方法
}

4.2. Attributes

Every attribute must have access modifiers added.

Must not use the keyword var to declare an attribute.

每条语句 一定不可 定义超过一个属性。

不该 使用下划线作为前缀,来区分属性是 protected 或 private。

以下是属性声明的一个范例:

<?php
namespace Vendor\Package;
class ClassName
{
 public $foo = null;
}

4.3方法

所有方法都 必须 添加访问修饰符。

不该 使用下划线作为前缀,来区分方法是 protected 或 private。

方法名称后 一定不可 有空格符,其开始花括号 必须 独占一行,结束花括号也 必须 在方法主体后单独成一行。参数左括号后和右括号前 一定不可 有空格。

一个标准的方法声明可参照以下范例,留意其括号、逗号、空格以及花括号的位置。

<?php
namespace Vendor\Package;
class ClassName
{
 public function fooBarBaz($arg1, &$arg2, $arg3 = [])
 {
 // method body
 }
}

4.4. 方法的参数

参数列表中,每个逗号后面 必须 要有一个空格,而逗号前面 一定不可 有空格。

有默认值的参数,必须 放到参数列表的末尾。

<?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 = []
 ) {
 // 方法的内容
 }
}

4.5. 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
 }
}

4.6. 方法及函数调用

方法及函数调用时,方法名或函数名与参数左括号之间 一定不可 有空格,参数右括号前也 一定不可 有空格。每个参数前 一定不可 有空格,但其后 必须 有一个空格。

<?php
bar();
$foo->bar($arg1);
Foo::bar($arg2, $arg3);
参数 可以 分列成多行,此时包括第一个参数在内的每个参数都 必须 单独成行。
<?php
$foo->bar(
 $longArgument,
 $longerArgument,
 $muchLongerArgument
);

5. 控制结构

控制结构的基本规范如下:

  • 控制结构关键词后 必须 有一个空格。
  • 左括号 ( 后 一定不可 有空格。
  • 右括号 ) 前也 一定不可 有空格。
  • 右括号 ) 与开始花括号 { 间 必须 有一个空格。
  • 结构体主体 必须 要有一次缩进。
  • 结束花括号 } 必须 在结构体主体后单独成行。

每个结构体的主体都 必须 被包含在成对的花括号之中,

这能让结构体更加结构话,以及减少加入新行时,出错的可能性。

5.1. if 、elseif 和 else

标准的 if 结构如下代码所示,请留意「括号」、「空格」以及「花括号」的位置,

注意 else 和 elseif 都与前面的结束花括号在同一行。

<?php
if ($expr1) {
 // if body
} elseif ($expr2) {
 // elseif body
} else {
 // else body;
}

应该 使用关键词 elseif 代替所有 else if ,以使得所有的控制关键字都像是单独的一个词。

5.2. switch 和 case

标准的 switch 结构如下代码所示,留意括号、空格以及花括号的位置。

case 语句 必须 相对 switch 进行一次缩进,而 break 语句以及 case 内的其它语句都 必须 相对 case进行一次缩进。

如果存在非空的 case 直穿语句,主体里 必须 有类似 // no break 的注释。

<?php
switch ($expr) {
 case 0:
 echo &#39;First case, with a break&#39;;
 break;
 case 1:
 echo &#39;Second case, which falls through&#39;;
 // no break
 case 2:
 case 3:
 case 4:
 echo &#39;Third case, return instead of break&#39;;
 return;
 default:
 echo &#39;Default case&#39;;
 break;
}

5.3. while 和 do while

一个规范的 while 语句应该如下所示,注意其「括号」、「空格」以及「花括号」的位置。

<?php
while ($expr) {
 // structure body
}

标准的 do while 语句如下所示,同样的,注意其「括号」、「空格」以及「花括号」的位置。

<?php
do {
 // structure body;
} while ($expr);

5.4. for

标准的 for 语句如下所示,注意其「括号」、「空格」以及「花括号」的位置。

<?php
for ($i = 0; $i < 10; $i++) {
 // for body
}

5.5. foreach

标准的 foreach 语句如下所示,注意其「括号」、「空格」以及「花括号」的位置。

<?php
foreach ($iterable as $key => $value) {
 // foreach body
}

5.6. try, catch

标准的 try catch 语句如下所示,注意其「括号」、「空格」以及「花括号」的位置。

<?php
try {
 // try body
} catch (FirstExceptionType $e) {
 // catch body
} catch (OtherExceptionType $e) {
 // catch body
}

6. 闭包

闭包声明时,关键词 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
);

【推荐课程:PHP视频教程

The above is the detailed content of Detailed introduction to PHP's PSR-2 coding style specification (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete