php 7开发规范,PHP 开发规范

1d13be22bfe8df58aa785bd41cbab156.png

486bfb78405564b3ecedf32de1f4fa1d.png

规范遵循PHP-FIG组织推荐的PHP开发标准(PSR)

PSR-1 是组织推荐的PHP开发规范

PSR-2 是组织推荐的代码风格

PSR-4 是组织推荐的类的自动加载规范

PSR-5 是组织推荐的文档注释规范

规范有点多,不一定记得住,所以第一期整理的规则是一些大家已经形成或者正在形成的路上的开发规范

规范的好处不用说了,提升团队协作的舒适度以及代码健壮性、安全性是毋庸置疑的。

PHP编程规约

命名规约

命名需要有意义,专有名词等特殊情况允许使用拼音(danyue)

// bad $a = 30; # 年龄

// good $age = 30; # 年龄

方法名采用首字母小写的驼峰方式,类名采用首字母大写的驼峰方式,方法名首词采用动词加驼峰

// bad

function calculate_salary(){

// method body

}

// good

function calculateSalary(){

// method body

}

// bad

class bizcode{

// class body

}

// good

class BizCode{

// class body

}

变量名都用驼峰

// good

$errorCode = 500;

// bad

$error_code = 500;

// 类的成员变量

class Code{

// good

private $conf;

// good

protected $business;

}

class Code{

// bad

private $_conf;

// bad

protected $_business;

}

`建议`Api返回值中的字段名,是驼峰的命名方式 这个根据应用场景处理。

-常量定义大写,单词间以下划线分隔

// bad

define("app_path", __DIR__ . "/../application/");

// good

define("APP_PATH", __DIR__ . "/../application/");

class BizCode{

// bad

const abc360 = 10;

// good

const LANDI = 20;

}

- PHP关键字都小写, 常量 true 、false 和 null 也都小写

// bad

FOREACH ($students AS $student) {

}

// good

foreach ($students as $student) {

}

// bad

IF ($age > 18) {

// condition body

} ELSE {

// condition body

}

// good

if ($age > 18) {

// condition body

} else {

// condition body

}

// bad

DEFINE("DEBUG", true);

// good

define("DEBUG", true);

?> 结束符在文件末尾省略

// bad

echo "Wow, non-blocking!";

?>

// good

echo "Wow, non-blocking!";

创建数组时统一使用 [] 代替 array()

// bad

$students = array();

// good

$students = [];

if/for/foreach/while/switch/do 等保留字与括号之间都必须加空格。

// bad

if($expr1){

// if body

}elseif($expr2) {

// elseif body

}else{

// else body;

}

// good

if ($expr1) {

// if body

} elseif ($expr2) {

// elseif body

} else {

// else body;

}

// bad

foreach($iterable as $key=>$value){

// foreach body

}

// good

foreach ($iterable as $key => $value) {

// foreach body

}

在一个 switch 块内,都必须包含一个 default 语句并且 放在最后,即使空代码。

// bad

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;

}

// good

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;

}

所有的类都必须添加创建者和创建日期

// bad

namespace libapi;

/**

* ApiController基类

* Class ApiController

* @package libapi

*/

class ApiController{

}

// good

/**

* User: xxx

* Date: 2019/01/20

* Time: 下午1:13

*/

namespace libapi;

/**

* ApiController基类

* Class ApiController

* @package libapi

*/

class ApiController{

}

namespace声明后得有个空行,use 代码块放一起且上下保留空白行

// bad

namespace VendorPackage;

use FooInterface;

use BarClass as Bar;

use OtherVendorOtherPackageBazClass;

// class declaration

// good

namespace VendorPackage;

use FooInterface;

use BarClass as Bar;

use OtherVendorOtherPackageBazClass;

// class declaration

extends implements 和类声明在一行

// bad

namespace VendorPackage;

use FooInterface;

use BarClass as Bar;

use OtherVendorOtherPackageBazClass;

class Foo extends Bar

implements FooInterface{

}

// good

namespace VendorPackage;

use FooInterface;

use BarClass as Bar;

use OtherVendorOtherPackageBazClass;

class Foo extends Bar implements FooInterface{

}

// 其他

类的起始大括号单独一行;类的结束大括号必须放在正文后面的下一行上

// bad

class Foo extends Bar implements FooInterface{

// class body

}

// good

class Foo extends Bar implements FooInterface{

// class body

}

方法参数在定义和传入时,多个参数逗号后边必须加空格, 函数参数默认值放最后, 方法参数给能够强制指定类型的指定类型

// bad

function fooBarBaz($arg3 = [], &$arg2, $arg1){

// method body

}

// good

function fooBarBaz($arg1, &$arg2, $arg3 = []){

// method body

}

abstract, final 放在类的可见性描述符前面,static放在描述符后面

// bad

abstract class ClassName{

static protected $foo;

protected abstract function zim();

static public final function bar(){

// method body

}

}

// good

abstract class ClassName{

protected static $foo;

abstract protected function zim();

final public static function bar(){

// method body

}

}

建议函数参数尽量不要超过4个, return 返回数据类型一致

方法、类、成员变量、复杂逻辑要注释清楚,注释遵循phpDocument

// bad

function foo($create_new){

if ($create_new) {

return new stdClass();

}

return null;

}

// good

/**

* @param bool $create_new When true returns a new stdClass.

*

* @return stdClass|null

*/

function foo($create_new){

if ($create_new) {

return new stdClass();

}

return null;

}

注释的双斜线与注释内容之间有且仅有一个空格

// bad

//this is a doc

// good

// this is a doc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值