Mediawiki.org的PHP编码约定

http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP

assignment作为expression来用看起来像个错误(looks surprising)

// No
if ( $a = foo() ) {
    bar();
}
// Yes
$a = foo();
if ( $a ) {
    bar();
}

为提高代码可读性,Mediawiki大量使用空格(spaces are cheap; you are a good typist)

二元运算符

// No
$a=$b+$c;
 
// Yes
$a = $b + $c;

函数名后面直接跟括号;括号内如有参数,两边都加空格

// Yes
$a = getFoo( $b );
$c = getBar();

控制结构 if while for foreach switch,关键字 catch,后面都有空格

// Yes
if ( isFoo() ) {
    $a = 'foo';
}
 
// No
if( isFoo() ) {
    $a = 'foo';
}

强制类型转换

// Yes
(int)$foo;
 
// No
(int) $bar;
( int )$bar;
( int ) $bar;

注释

// Yes: Proper inline comment
//No: Missing space

三元运算符

除非表达式很短,否则用 if。记住一切都为了代码可读性。

// "if" is English; ?: is not.

 

posted @ 2014-11-10 18:32  crepesofwrath  阅读(232)  评论(1编辑  收藏  举报