免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1429 | 回复: 1
打印 上一主题 下一主题

PHP代码样式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-17 14:20 |只看该作者 |倒序浏览
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 */  

  10. // 错误
  11. <?
  12. echo "Here's my code!";
  13. ?>
  14. // 正确
  15. <?php
  16. echo "Here's my code!";

  17. /* End of file mycode.php */
复制代码
代码缩进采用4空格,禁止使用Tab作为代码缩进,单行长度为80个字符,最大不要超过120个字符。

字符串中没有变量的统一采用单引号括起来,对于含有变量的字符串,变量应写做
  1. // 错误   
  2. $greeting = "Welcome ${name}!";   
  3. // 正确   
  4. $greeting = "Welcome {$name}!";  

  5. // 错误
  6. $greeting = "Welcome ${name}!";
  7. // 正确
  8. $greeting = "Welcome {$name}!";
复制代码
类和方法
类名和方法名均使用驼峰式命名法,只是类名首字母大写,方法名首字母小写,命名应该保持意思精准短小。
  1. // 错误   
  2. class superclass   
  3. class superClass   
  4. // 正确   
  5.   
  6. class SuperClass  

  7. // 错误
  8. class superclass
  9. class superClass
  10. // 正确

  11. class SuperClass
复制代码
  1. // 错误   
  2. function fileproperties() // 意思不清楚并且没有驼峰式命名   
  3. function fileProperties() // 意思不清楚   
  4. function getfileproperties() // 好些了,但没有驼峰式命名   
  5. // 正确   
  6. function getFileProperties()  

  7. // 错误
  8. function fileproperties() // 意思不清楚并且没有驼峰式命名
  9. function fileProperties() // 意思不清楚
  10. function getfileproperties() // 好些了,但没有驼峰式命名
  11. // 正确
  12. 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  

  13. // 错误
  14. $i = "foobar";  // 单字符变量只充许使用在for循环中
  15. $Str                // 包含大写字符
  16. $bufferdText   // 驼峰式变量,并且意思可以再精简些
  17. $groupid        // 两个单词之间需要下划线分开
  18. $name_of_last_city_used // 太长
  19. // 正确
  20. for ($i = 0; $i < 10; $i++)
  21. $str
  22. $buffer
  23. $group_id
  24. $last_city
复制代码
常量
常量名统一采用大写字母与下划线方式,在命名时也应该保持意思精准短小,也应该避免使用单字符命名。
  1. // 错误   
  2. MyConstant       // 应该用下划线并且字母没有全大写   
  3. N                     // 单字符   
  4. S_C_VER           // 意思不清楚   
  5. // 正确   
  6. MY_CONSTANT   
  7. NEWLINE   
  8. SUPER_CLASS_VERSION  

  9. // 错误
  10. MyConstant       // 应该用下划线并且字母没有全大写
  11. N                     // 单字符
  12. S_C_VER           // 意思不清楚
  13. // 正确
  14. MY_CONSTANT
  15. NEWLINE
  16. 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. */  

  12. /**
  13. * 文件的简短描述
  14. *
  15. * 文件的详细描述(如果有的话)……
  16. *
  17. * LICENSE: 一些 license 信息
  18. *
  19. * @copyright  Copyright (c) 2012 Company
  20. * @license    BSD License
  21. * @since      File available since Release 1.0
  22. */
复制代码
每个类必须至少包含这些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. */  

  11. /**
  12. * 类的简述
  13. *
  14. * 类的详细描述 (如果有的话)... ...
  15. *
  16. * @copyright  Copyright (c) 2012 Company
  17. * @license    BSD License
  18. * @since      Class available since Release 1.0
  19. * @deprecated Class deprecated in Release 2.0
  20. */
复制代码
每个方法必须至少包含这些phpDocumentor标签:
  1. /**  
  2. * 方法的详细描述  
  3. *  
  4. * @param   string  
  5. * @return  string  
  6. */  

  7. /**
  8. * 方法的详细描述
  9. *
  10. * @param        string
  11. * @return        string
  12. */


  13. 代码中的单行注释统一使用双斜线"//"方式
复制代码
  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);  

  13. // break up the string by newlines
  14. $parts = explode("\n", $str);

  15. // A longer comment that needs to give greater detail on what is
  16. // occurring and why can use multiple single-line comments.  Try to
  17. // keep the width reasonable, around 70 characters is the easiest to
  18. // read.  Don't hesitate to link to permanent external resources
  19. // that may provide greater detail:
  20. //
  21. // http://example.com/information_about_something/in_particular/

  22. $parts = $this->foo($parts);


  23. 参考:CodeIgniter,ZendFramework,Wordpress等
  24. http://codeigniter.com/user_guide/general/styleguide.html
  25. http://framework.zend.com/manual/zh/coding-standard.coding-style.html
  26. http://codex.wordpress.org/WordPress_Coding_Standards
复制代码
其他
Javascript 代码样式参见(英文) http://google-styleguide.googlec ... javascriptguide.xml

最后更新 2012/2/17

论坛徽章:
0
2 [报告]
发表于 2012-02-17 22:08 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP