E_STRICT-compatible code
Starting on 01 January 2007, all new code that is suggested for inclusion into PEAR must be E_STRICT-compatible. This means that it must not produce any warnings or errors when PHP’s error reporting level is set to E_ALL | E_STRICT. 这个也是关于与pear社区互动所需要满足的条件:虽然php可以在E_NOTICE/E_WARNING下工作,但是应该控制代码少报出这样的错误——至少要兼容E_STRICT级别的错误,也就是在error_reporting(E_ALL | E_STRICT)的情况下不报出警告。
Error Handling Guidelines
定义:An error is defined as an unexpected, invalid program state from which it is impossible to recover.
这块说的是php5及以上版本的Exception标准。485系统基本没有exception逻辑,ecstore入口kernel.php有一个大exception:
try{ require(ROOT_DIR.'/config/mapper.php'); ... self::$__router = app::get($app)->router(); self::$__router->dispatch($path); } catch (Exception $e){}
个人觉得exception不该频繁的用,PHP应该什么时候使用 Exception ? 它的性能如何?风雪之隅对此怎么说呢?
Best Practices,最佳实践
There are other things not covered by PEAR Coding Standards which are mostly subject of personal preference and not directly related to readability of the code.The only recommendation could be made to keep consistency within package and respect personal style of other developers. 这里的内容并不包含在PEAR CS内,而是一些经验主义的建议,目的是保持一致的代码风格同时又尊重每个开发者的个性。
- Readability of code block
Related lines of code should be grouped into blocks, separated from each other to keep readability as high as possible:
if ($foo) {
$bar = 1;
}
if ($spam) {
$ham = 1;
}
if ($pinky) {
$brain = 1;
}
下面这段代码相对就更容易读,容易帮读者(代码应该像流水账小说一样)思路清晰:
// comment
if ($foo) {
$bar = 1;
}
// comment
if ($spam) {
$ham = 1;
}
// comment
if ($pinky) {
$brain = 1;
}
- Return earlier
To keep readability in functions and methods, it is wise to return early if simple conditions apply that can be checked at the beginning of a method. 让方法尽可能的早返回!这一点对代码可读性、缩进美观非常非常有用!
来自于485的一个段代码:
function remove(){ $objOrder = &$this->system->loadModel('trading/order'); if(is_array($_POST['items']['items'])){ foreach($_POST['items']['items'] as $orderid){ if(!$objOrder->toRemove($orderid, $message)){ echo $message; exit; } } echo __('删除成功;'); }else{ echo __('没有选中记录;'); } }
可以这么写:
function remove(){ $orderMdl =& $this->system->loadModel('trading/order'); if( ! is_array($_POST['items']['items']) ) { echo __('没有选中记录;'); return; } foreach($_POST['items']['items'] as $orderid){ if( !$objOrder->toRemove($orderid, $message) ) { echo $message; return; } } echo __('删除成功;'); }
if ( $var == ‘something’){}?
最好写成这样:if ( ‘something’ == $var ){},有时候一个=丢掉了成了赋值表达式,这很难排查,而对一个常量赋值会导致parse error,这很明显。
(int) $var OR intval($var) OR +$var?
(int) 、intval将后面的表达式变成整形,(int)比intval好多了,避开函数调用的代价。
$var = +’20.03′,得到 $var === 20.03. $var = +0×12; 得到$var=18. 这个方式应该是最便捷最高效的。
for($i=0; $i<count($array); $i++);?
这么写会更好:
for($i=count($array)-1,$i>=0;$i–); OR
for($i=0,$I=count($array); $i<$I;$i++);
The PEAR toolbox
PEAR provides some tools to help developers keep their code clean and free of coding standards related errors. 小布什说道,人类最伟大的发明是将权利关进笼子。PEAR也提供了一些工具来约束代码使其符合规范。
For one there is PHP_CodeSniffer which can be used to detect coding standard errors in your scripts.