- 论坛徽章:
- 0
|
PHP代码样式
phpcoding style.
如果使用第三方框架,并且有提供代码样式说明,则遵循其代码样式标准,否则如下!
文件格式
文件统一使用UTF-8编码,LF(Unix)换行符。
文件名统一采用小写字母+下划线格式,如果是PHP类文件,文件名应与类名相同(如:文件名为form_validation.php,相对应的类为"FormValidation")。
代码格式
代码必须使用标准的PHP标签定界,禁止使用短标签(<? //... ?>),对于只包含有PHP代码的文件,禁止使用PHP结束标志("?>"),文件末尾使用注释说明"/* End of file <filename.php> */"。这样做,可以防止它的末尾的被意外地注入相应。- 1.// 错误
- 2.<?
- 3.echo "Here's my code!";
- 4.?>
- 5.// 正确
- 6.<?php
- 7.echo "Here's my code!";
- 8.
- 9./* End of file mycode.php */
-
复制代码 代码缩进采用4空格,禁止使用Tab作为代码缩进,单行长度为80个字符,最大不要超过120个字符。
字符串中没有变量的统一采用单引号括起来,对于含有变量的字符串,变量应写做
1- .// 错误
- 2.$greeting = "Welcome ${name}!";
- 3.// 正确
- 4.$greeting = "Welcome {$name}!";
-
复制代码 类和方法
类名和方法名均使用驼峰式命名法,只是类名首字母大写,方法名首字母小写,命名应该保持意思精准短小。- 1.// 错误
- 2.class superclass
- 3.class superClass
- 4.// 正确
- 5.
- 6.class SuperClass
-
复制代码- 1.// 错误
- 2.function fileproperties() // 意思不清楚并且没有驼峰式命名
- 3.function fileProperties() // 意思不清楚
- 4.function getfileproperties() // 好些了,但没有驼峰式命名
- 5.// 正确
- 6.function getFileProperties()
复制代码 变量
变量名统一使用小写字母,单词之间用下划线分割,也应该保持意思精准短小,禁止使用臃肿的变量名,亦禁止使用单字符做为局部(或全局)变量(如$i),在for循环中除外。- 1.// 错误
- 2.$i = "foobar"; // 单字符变量只充许使用在for循环中
- 3.$Str // 包含大写字符
- 4.$bufferdText // 驼峰式变量,并且意思可以再精简些
- 5.$groupid // 两个单词之间需要下划线分开
- 6.$name_of_last_city_used // 太长
- 7.// 正确
- 8.for ($i = 0; $i < 10; $i++)
- 9.$str
- 10.$buffer
- 11.$group_id
- 12.$last_city
复制代码 常量
常量名统一采用大写字母与下划线方式,在命名时也应该保持意思精准短小,也应该避免使用单字符命名。- 1.// 错误
- 2.MyConstant // 应该用下划线并且字母没有全大写
- 3.N // 单字符
- 4.S_C_VER // 意思不清楚
- 5.// 正确
- 6.MY_CONSTANT
- 7.NEWLINE
- 8.SUPER_CLASS_VERSION
复制代码 注释
所有文档块 必须和phpDocumentor格式兼容,phpDocumentor格式的描述超出了本文档的范围,请参考: http://phpdoc.org/
每个包含PHP代码的文件必须至少在文件顶部含这些phpDocumentor标签:- 1./**
- 2. * 文件的简短描述
- 3. *
- 4. * 文件的详细描述(如果有的话)……
- 5. *
- 6. * LICENSE: 一些 license 信息
- 7. *
- 8. * @copyright Copyright (c) 2012 Company
- 9. * @license BSD License
- 10. * @since File available since Release 1.0
- 11.*/
-
复制代码 每个类必须至少包含这些phpDocumentor标签:- 1./**
- 2. * 类的简述
- 3. *
- 4. * 类的详细描述 (如果有的话)... ...
- 5. *
- 6. * @copyright Copyright (c) 2012 Company
- 7. * @license BSD License
- 8. * @since Class available since Release 1.0
- 9. * @deprecated Class deprecated in Release 2.0
- 10. */
-
复制代码 每个方法必须至少包含这些phpDocumentor标签:- 1./**
- 2. * 方法的详细描述
- 3. *
- 4. * @param string
- 5. * @return string
- 6. */
复制代码 代码中的单行注释统一使用双斜线"//"方式- 1.// break up the string by newlines
- 2.$parts = explode("\n", $str);
- 3.
- 4.// A longer comment that needs to give greater detail on what is
- 5.// occurring and why can use multiple single-line comments. Try to
- 6.// keep the width reasonable, around 70 characters is the easiest to
- 7.// read. Don't hesitate to link to permanent external resources
- 8.// that may provide greater detail:
- 9.//
- 10.// http://example.com/information_about_something/in_particular/
- 11.
- 12.$parts = $this->foo($parts);
-
复制代码 参考:CodeIgniter,ZendFramework,Wordpress等
http://codeigniter.com/user_guide/general/styleguide.html
http://framework.zend.com/manual ... d.coding-style.html
http://codex.wordpress.org/WordPress_Coding_Standards
其他
Javascript 代码样式参见(英文) http://google-styleguide.googlec ... javascriptguide.xml
最后更新 2012/2/17 |
|