常用代码规范
所有 PSR 规范请见:https://learnku.com/docs/psr
想细看请点击下面链接, 想偷懒请看下面demo
来自PSR-1
命名空间以及类的命名必须遵循PSR-4
类的命名 必须 遵循 StudlyCaps
大写开头的驼峰命名规范。
StudlyCaps
大写开头的驼峰命名规范。 类的常量中所有字母都 必须 大写,词间以下划线分隔。
类的属性命名建议 遵循:小写开头的驼峰式($camelCase)
方法名称 必须 符合 camelCase()
式的小写开头驼峰命名规范。
camelCase()
式的小写开头驼峰命名规范。<?php
// PHP 5.3及以后版本的写法
namespace Vendor\Model;
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
}
来自PSR-2
namespace
声明后 必须 插入一个空白行。
namespace
声明后 必须 插入一个空白行。 所有 use
必须 在 namespace
后声明。
use
必须 在 namespace
后声明。 每条 use
声明语句 必须 只有一个 use
关键词。
use
声明语句 必须 只有一个 use
关键词。 use
声明语句块后 必须 要有一个空白行
use
声明语句块后 必须 要有一个空白行 关键词 extends
和 implements
必须 写在类名称的同一行。
extends
和 implements
必须 写在类名称的同一行。 类的开始花括号 必须 独占一行,结束花括号也 必须 在类主体后独占一行。
implements
的继承列表也 可以 分成多行,这样的话,每个继承接口名称都 必须 分开独立成行,包括第一个。
implements
的继承列表也 可以 分成多行,这样的话,每个继承接口名称都 必须 分开独立成行,包括第一个。 每个属性都 必须 添加访问修饰符。
所有方法都 必须 添加访问修饰符。
参数列表中,每个逗号后面 必须 要有一个空格,而逗号前面 一定不可 有空格。
需要添加 abstract
或 final
声明时,必须 写在访问修饰符前,而 static
则 必须 写在其后。
abstract
或 final
声明时,必须 写在访问修饰符前,而 static
则 必须 写在其后。 方法及函数调用时,方法名或函数名与参数左括号之间 一定不可 有空格,参数右括号前也 一定不可 有空格。每个参数前 一定不可 有空格,但其后 必须 有一个空格
后面不写了 看下面例子吧。!!!!!
<?php
namespace Vendor\Package;
use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;
class Foo extends Bar implements
FooInterface
\ArrayAccess,
\Countable,
\Serializable
{
public $foo = null;
protected static $foo;
public function sampleFunction($a, $b = null)
{
if ($a === $b) {
bar();
} elseif ($a > $b) {
$foo->bar($arg1);
} else {
BazClass::bar($arg2, $arg3);
}
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 ($expr) {
// structure body
}
do {
// structure body;
} while ($expr);
for ($i = 0; $i < 10; $i++) {
// for body
}
foreach ($iterable as $key => $value) {
// foreach body
}
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
}
$closureWithArgs = function ($arg1, $arg2) {
// body
};
$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
// body
};
$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
};
$foo->bar(
$arg1,
function ($arg2) use ($var1) {
// body
},
$arg3
);
}
final public static function bar()
{
// 方法的内容
}
public function aVeryLongMethodName(
ClassTypeHint $arg1,
&$arg2,
array $arg3 = []
) {
// 方法的内容
}
abstract protected function zim();
final public static function bar()
{
// method body
}
}
bar(); //方法调用
$foo->bar($arg1);
Foo::bar($arg2, $arg3);
Last updated
Was this helpful?