php代码规范

基本规范

PHP-FIG:PSR

PSR基本规范中文翻译

命名规则

  • 全局变量以g_开头

  • 变量使用驼峰式,deleteArticle

  • 常量中所有字母都必须大写,词间以下划线分隔。UESTC_ROOT

  • 类名应以大写字母开头,每个单词的首字母大写。ActionController

  • 是数组的变量,在最后必须要使用List或者Array变量注明。valueList

编码规范

  • 源码文件必须采用UTF-8编码,且不得有BOM头

  • 缩进采用soft tab,使用4个空格

  • 所有的全局变量应该写在函数的最开头,并且和后面的代码以空行隔开

  • 对于函数返回值的判断,特别是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的嵌套。

减少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++

注释规范

  • 函数必须通过param和return标记指明其参数和返回值
  • 必要的地方使用非文档性注释(也就是“//”这种),提高代码易读性

参考以下两个示例

 <?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)
	{
		//。。。。。
	}

}

posted @ 2017-03-03 14:05  jcuan  阅读(199)  评论(0编辑  收藏  举报