SQL 过长
// heredoc语法 $sql = <<<SQL SELECT delivery_id FROM d_test WHERE delivery_id IN (123,234) GROUP BY delivery_id HAVING SUM(send_number) <= 0; SQL;
if 等控制结构条件过长
if ($a > 0 && $b > 0 && $c > 0 && $d > 0 && $e > 0) { }
方法或函数参数大于三个换行
public function tooLangFunction( $valueOne = '', $valueTwo = '', $valueThree = '', $valueFour = '', $valueFive = '', $valueSix = '') { //coding... }
链式操作超过两个
$this->nameTest->functionOne() ->functionTwo() ->functionThree();
数组 Php 5.4
以后,使用 []
$a = [ 'aaa' => 'aaa', 'bbb' => 'bbb' ];
单引号多引号
$str = 'str'; $arg = "$str";
声明类或者方法或函数添加描述&属性描述&作者
/** * 类描述 * * desc */ class StandardExample { /** * 常量描述. * * @var string */ const THIS_IS_A_CONST = ''; /** * 属性描述. * * @var string */ public $nameTest = ''; /** * 构造函数. * * 构造函数描述 * @author name* @param string $value 形参名称/描述 * @return 返回值类型 返回值描述 * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值) */ public function __construct($value = '') { // coding... }
api 方法提供测试样例 example
/** * 成员方法名称. * * 成员方法描述 * * @param string $value 形参名称/描述 * * @example domain/api/controller/action?argu1=111&argu2=222 */ public function testFunction($value = '') { // code... }
使用 try…catch…
try { // coding... } catch (\Exception $e) { // coding... }
连续调用多个方法(大于3个)使用 foreach
// 改写doSome为doSomething class StandardExample { /** * 方法列表 * * @var array */ private $_functionList = []; public function __construct($functionList = array()) { $this->_functionList = $value; } public function doSome() { $this->functionOne(); $this->functionTwo(); $this->functionThree(); $this->functionFour(); } public function doSomething() { foreach($this->_functionList as $function) { $this->$function(); } } ... }
文件顶部进行版权声明
// +---------------------------------------------------------------------- // | Company Name xx服务 // +---------------------------------------------------------------------- // | Copyright (c) 2017 https://www.pianyiwan.com All rights reserved. // +---------------------------------------------------------------------- // | Author: name <email> // +----------------------------------------------------------------------