php代码样式
如果使用第三方框架,并且有提供代码样式说明,则遵循其代码样式标准,否则如下!
== 格式:
* 使用utf-8编码
* 使用4个空格缩进,禁止使用tab
* unix样式换行符(lf)
* 在逗号、冒号和分号操作符使用空格
* 在 (,[ 后面、],) 前面不使用空格
* 在代码块中使用4个空格缩进
* 采用层级缩进
* 在return方法的返回值前使用空行(除非只有一行代码)以及在两个方法中使用空行区分
* 在两个大的逻辑代码段之间使用空行区分
* 保持行字数在80字以内,最多不超过120
* 使用标准的php标签定界,禁止使用短标签( //... ?>),对于只包含有php代码的文
件,禁止使用php结束标志(?>),文件末尾使用注释说明/* end of file */
* 单行代码也必须使用分号(;)结束
== 命名:
* 文件名使用snake_case方式,禁止使用臃肿的文件名
* 变量名使用snake_case方式,禁止使用臃肿的变量名
* 禁止使用单字符做为局部变量(如$i),在for循环中除外
* 禁止使用大写字母做为全局变量,如使用大写字母,应使用screaming_snake_case方式
* 类名使用camelcase方式,方法名使用camelcase方式(保持像http,rfc,xml缩写词的大写)
* 常量名使用screaming_snake_case方式
// badsuperclass.phpsuperclass.phpsuperclass.php$i = foobar; // 单字符变量只充许使用在for循环中$bufferdtext // 驼峰式变量,并且意思可以再精简些$groupid // 两个单词之间需要下划线分开$name_of_last_city_used // 太长myconstant // 应该用下划线并且字母没有全大写n // 单字符s_c_ver // 意思不清楚class superclassclass superclassfunction fileproperties() // 意思不清楚并且没有驼峰式命名function fileproperties() // 意思不清楚function getfileproperties() // 好些了,但没有驼峰式命名// goodsuper_class.phpfor ($i = 0; $i ...
== 注释:
* 文档块必须和phpdocumentor格式兼容,请参考: http://phpdoc.org/
* 避免多余的意见
/** * 控制器类说明信息 */class controller { private static $instance; public function __construct() { ... } /** * 函数说明信息 */ public static function &get_instance() { ... }/* end of file controller.php */
== 其他:
* 保持代码简单
* 保持一致性
参考:codeigniter,zendframework,wordpress等
http://codeigniter.com/user_guide/general/styleguide.html
http://framework.zend.com/manual/zh/coding-standard.coding-style.html
http://codex.wordpress.org/wordpress_coding_standards
其他
github https://github.com/styleguide
html/css 代码样式(英文)
http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml
javascript 代码样式(英文)
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
ruby 样式参见(英文) https://github.com/chneukirchen/styleguide/blob/master/ruby-style
1 楼 huawei2772 2012-02-17 好!