PHP-FIG:PSRphp
PSR基本规范中文翻译html
全局变量以g_开头git
变量使用驼峰式,deleteArticlegithub
常量中全部字母都必须大写,词间如下划线分隔。UESTC_ROOT数组
类名应以大写字母开头,每一个单词的首字母大写。ActionController安全
是数组的变量,在最后必需要使用List或者Array变量注明。valueList框架
源码文件必须采用UTF-8编码,且不得有BOM头ide
缩进采用soft tab,使用4个空格函数
全部的全局变量应该写在函数的最开头,而且和后面的代码以空行隔开codeigniter
对于函数返回值的判断,特别是true/false, 必须用===或!==
字符串尽可能用’ ‘而不是” “进行引用,一个是效率问题,一个是安全问题
if/while等结构体,即便只有一行,也必须加上花括号,不得写成一行
一个函数不得超过300行,建议控制在100行之内。
数组使用[] 不要使用array() 。初始化array若是采用多行结构时,数据项部分须要缩进,且最后一个数据项后面的逗号不可省略,这是为了后续便于添加
$a=[ 'a'=>1, 'b'=>2, ];
除模板外,不容许使用?>标记结尾, 避免其后误加的字符干扰页面渲染
类的开始花括号 { 必须写在其声明后自成一行,结束花括号 } 也必须写在其主体后自成一行。
方法的开始花括号 { 必须写在函数声明后自成一行,结束花括号 } 也必须写在函数主体后自成一行。
class Add { final public static function getApple() { } }
类的属性和方法必须添加访问修饰符(private、protected 以及 public), abstract 以及 final 必须声明在访问修饰符以前,而 static 必须声明在访问修饰符以后。每一个类的属性也必须添加访问修饰符。
常量 true 、false 和 null 必须所有小写。
参数列表中,每一个逗号后面必须要有一个空格,而逗号前面必定不能有空格。
其余示例
<?php if ($expr1) { // if body } elseif ($expr2) { // elseif body } else { // else body; } 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 } try { // try body } catch (OtherExceptionType $e) { // catch body }
减小if/else嵌套, 更利于代码逻辑的理解。
不建议的方式(最好不要采用,而且条件中代码块超过10行的不得采用本方式): if (a === false) { // error handle } else { if (b === false) { // handle } } 推荐的方式: if (a === false) { // error handle } if (b === false) { // handle }
全部文件路径都须要利用框架提供的宏写成绝对路径
文件更新操做,必须使用临时文件+mv的方式,切忌直接写在原文件
错误码使用统一文件集中配置,而且使用常量,而不该裸写数字
把重复调用放在循环体外。
不推荐形式: for($i = 0; $i < count($arr); $i++) 推荐形式: $arrCount = count($arr); for($i = 0; $i < $arrCount; $i++
参考如下两个示例
<?php /** * file summary. * @version 1.1.1 */ /** * A summary informing the user what the associated element does 函数总结 * * A *description*, that can span multiple lines, to go _in-depth_ into the details of this element * and to provide some background information or textual references 较详细的描述 * * @author XXX * @param string $var1 With a *description* of this argument, these may also * span multiple lines * @param string(类型) $var2(名称) 参数2的描述 * @return void */ function myFunction($var1,$var2) { } /** * summary for this function * * @deprecated 1.0.0 No longer used by internal code and not recommended(对于之后将被移除或废弃的函数须要注明). * @todo remove this function 之后要作的事情 * @return void */ function dead() { }
类的示例
<?php /** * CodeIgniter * * @author EllisLab Dev Team * @license http://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 1.0.0 */ /** * Zip Compression Class * * This class is based on a library I found at Zend: * http://www.zend.com/codex.php?id=696&single=1 * * The original library is a little rough around the edges so I * refactored it and added several additional methods -- Rick Ellis * * @package CodeIgniter * @subpackage Libraries * @category Encryption * @author EllisLab Dev Team * @link https://codeigniter.com/user_guide/libraries/zip.html */ class CI_Zip { /** * Zip data in string form * * @var string */ public $zipdata = ''; /** * Initialize zip compression class * * @return void */ public function __construct() { //...... } /** * Add Directory * * Lets you add a virtual directory into which you can place files. * * @param mixed $directory the directory name. Can be string or array * @return void */ public function add_dir($directory) { //。。。。。 } }