PHP 开发规范
规范遵循PHP-FIG组织推荐的PHP开发标准(PSR)
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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;
// 类的成员变量classCode{// goodprivate$conf;// goodprotected$business;}
classCode{// badprivate$_conf;// badprotected$_business;}
- `建议`Api返回值中的字段名,是驼峰的命名方式 这个根据应用场景处理
- 常量定义大写,单词间以下划线分隔
// baddefine('app_path', __DIR__ . '/../application/');// gooddefine('APP_PATH', __DIR__ . '/../application/');classBizCode{// badconstabc360 = 10;// goodconstLANDI = 20;}
- PHP关键字都小写, 常量 true 、false 和 null 也都小写
// badFOREACH ($studentsAS $student) {}// goodforeach($studentsas$student) {}// badIF ($age> 18) {// condition body} ELSE {// condition body}// goodif($age> 18) {// condition body} else{// condition body}// badDEFINE('DEBUG', true);// gooddefine('DEBUG', true);
- `?>` 结束符在文件末尾省略
// bad<?phpecho'Wow, non-blocking!';?>// good<?phpecho'Wow, non-blocking!';
- 创建数组时统一使用 [] 代替 array()
// bad$students= array();// good$students= [];
- if/for/foreach/while/switch/do 等保留字与括号之间都必须加空格。
// badif($expr1){// if body}elseif($expr2) {// elseif body}else{// else body;}
// goodif($expr1) {// if body} elseif($expr2) {// elseif body} else{// else body;}// badforeach($iterableas$key=>$value){// foreach body}// goodforeach($iterableas$key=> $value) {// foreach body}
- 在一个 switch 块内,都必须包含一个 default 语句并且 放在最后,即使空代码
// badswitch($expr) {case0:echo'First case, with a break';break;case1:echo'Second case, which falls through';// no breakcase2:case3:case4:echo'Third case, return instead of break';return;}// goodswitch($expr) {case0:echo'First case, with a break';break;case1:echo'Second case, which falls through';// no breakcase2:case3:case4:echo'Third case, return instead of break';return;default:echo'Default case';break;}
- 所有的类都必须添加创建者和创建日期
// bad<?phpnamespacelib\api;/*** ApiController基类* Class ApiController* @package lib\api*/classApiController{}
// good<?php/*** User: xxx* Date: 2019/01/20* Time: 下午1:13*/namespacelib\api;/*** ApiController基类* Class ApiController* @package lib\api*/classApiController{}
- namespace声明后得有个空行,use 代码块放一起且上下保留空白行
// badnamespaceVendor\Package;useFooInterface;
useBarClass asBar;
useOtherVendor\OtherPackage\BazClass;// class declaration// goodnamespaceVendor\Package;useFooInterface;useBarClass asBar;useOtherVendor\OtherPackage\BazClass;// class declaration
- extends implements 和类声明在一行
// badnamespaceVendor\Package;
useFooInterface;useBarClass asBar;useOtherVendor\OtherPackage\BazClass;
classFoo extendsBarimplementsFooInterface{}// goodnamespaceVendor\Package;
useFooInterface;useBarClass asBar;useOtherVendor\OtherPackage\BazClass;
classFoo extendsBar implementsFooInterface{}// 其他
- 类的起始大括号单独一行;类的结束大括号必须放在正文后面的下一行上
// badclassFoo extendsBar implementsFooInterface {// class body}// goodclassFoo extendsBar implementsFooInterface{// class body}
- 方法参数在定义和传入时,多个参数逗号后边必须加空格, 函数参数默认值放最后, 方法参数给能够强制指定类型的指定类型
// badfunctionfooBarBaz($arg3= [], &$arg2, $arg1){// method body}// goodfunctionfooBarBaz($arg1, &$arg2, $arg3= []){// method body}
- abstract, final 放在类的可见性描述符前面,static放在描述符后面
// badabstractclassClassName{staticprotected$foo;protectedabstractfunctionzim();staticpublicfinalfunctionbar(){// method body}}
// goodabstractclassClassName{protectedstatic$foo;
abstractprotectedfunctionzim();
finalpublicstaticfunctionbar(){// method body}}
- `建议`函数参数尽量不要超过4个, return 返回数据类型一致
- 方法、类、成员变量、复杂逻辑要注释清楚,注释遵循phpDocument
// badfunctionfoo($create_new){if($create_new) {returnnewstdClass();}returnnull;}// good/*** @param bool $create_new When true returns a new stdClass.** @return stdClass|null*/functionfoo($create_new){if($create_new) {returnnewstdClass();}returnnull;}
- 注释的双斜线与注释内容之间有且仅有一个空格
// bad//this is a doc// good// this is a doc
其他
- 开发服务时遵循SRP-职责单一,ISP-接口隔离原则,DRY -无重复代码原则, 接口业务单一。
- 框架内部提供了Session方法,一定要用框架的方法,勿直接使用$_SESSION设置
- 在高并发场景中,避免使用”等于”判断作为中断或退出的条件
- 非特殊情况,忌循环查询数据库(需要说明场景),涉及到数据库写操作,要有事务保证
- 生产环境禁止输出 debug 日志
- 禁止向 HTML 页面输出未经安全过滤或未正确转义的用户数据
- 在使用平台资源,譬如短信、邮件、电话、下单、支付,必须实现正确的防重放的机 制,如数量限制、疲劳度控制、验证码校验,避免被滥刷而导致资损
- 对程序算法的复杂度要时刻考虑着,尤其是时间复杂度
- 所有sql语句必须被包装到model类中不允许出现在controller,service等地方
- 数据关联查询禁用出现与本模块或其它业务的关联查询
工具统一
- PHPSTORM
- 统一IDE模板
更多精彩

