免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1482 | 回复: 1

PHP代码样式 [复制链接]

论坛徽章:
0
发表于 2012-02-17 15:55 |显示全部楼层

PHP代码样式


phpcoding style.

如果使用第三方框架,并且有提供代码样式说明,则遵循其代码样式标准,否则如下!

文件格式
文件统一使用UTF-8编码,LF(Unix)换行符。

文件名统一采用小写字母+下划线格式,如果是PHP类文件,文件名应与类名相同(如:文件名为form_validation.php,相对应的类为"FormValidation")。

代码格式
代码必须使用标准的PHP标签定界,禁止使用短标签(<? //... ?>),对于只包含有PHP代码的文件,禁止使用PHP结束标志("?>"),文件末尾使用注释说明"/* End of file <filename.php> */"。这样做,可以防止它的末尾的被意外地注入相应。
  1. 1.// 错误  
  2. 2.<?  
  3. 3.echo "Here's my code!";  
  4. 4.?>  
  5. 5.// 正确  
  6. 6.<?php  
  7. 7.echo "Here's my code!";  
  8. 8.  
  9. 9./* End of file mycode.php */  
复制代码
代码缩进采用4空格,禁止使用Tab作为代码缩进,单行长度为80个字符,最大不要超过120个字符。

字符串中没有变量的统一采用单引号括起来,对于含有变量的字符串,变量应写做




1
  1. .// 错误  
  2. 2.$greeting = "Welcome ${name}!";  
  3. 3.// 正确  
  4. 4.$greeting = "Welcome {$name}!";  
复制代码
类和方法
类名和方法名均使用驼峰式命名法,只是类名首字母大写,方法名首字母小写,命名应该保持意思精准短小。
  1. 1.// 错误  
  2. 2.class superclass  
  3. 3.class superClass  
  4. 4.// 正确  
  5. 5.  
  6. 6.class SuperClass  
复制代码
  1. 1.// 错误  
  2. 2.function fileproperties() // 意思不清楚并且没有驼峰式命名  
  3. 3.function fileProperties() // 意思不清楚  
  4. 4.function getfileproperties() // 好些了,但没有驼峰式命名  
  5. 5.// 正确  
  6. 6.function getFileProperties()  
复制代码
变量
变量名统一使用小写字母,单词之间用下划线分割,也应该保持意思精准短小,禁止使用臃肿的变量名,亦禁止使用单字符做为局部(或全局)变量(如$i),在for循环中除外。
  1. 1.// 错误  
  2. 2.$i = "foobar";  // 单字符变量只充许使用在for循环中  
  3. 3.$Str                // 包含大写字符  
  4. 4.$bufferdText   // 驼峰式变量,并且意思可以再精简些  
  5. 5.$groupid        // 两个单词之间需要下划线分开  
  6. 6.$name_of_last_city_used // 太长  
  7. 7.// 正确  
  8. 8.for ($i = 0; $i < 10; $i++)  
  9. 9.$str  
  10. 10.$buffer  
  11. 11.$group_id  
  12. 12.$last_city  
复制代码
常量
常量名统一采用大写字母与下划线方式,在命名时也应该保持意思精准短小,也应该避免使用单字符命名。
  1. 1.// 错误  
  2. 2.MyConstant       // 应该用下划线并且字母没有全大写  
  3. 3.N                     // 单字符  
  4. 4.S_C_VER           // 意思不清楚  
  5. 5.// 正确  
  6. 6.MY_CONSTANT  
  7. 7.NEWLINE  
  8. 8.SUPER_CLASS_VERSION  
复制代码
注释
所有文档块 必须和phpDocumentor格式兼容,phpDocumentor格式的描述超出了本文档的范围,请参考: http://phpdoc.org/

每个包含PHP代码的文件必须至少在文件顶部含这些phpDocumentor标签:
  1. 1./**
  2. 2. * 文件的简短描述
  3. 3. *
  4. 4. * 文件的详细描述(如果有的话)……
  5. 5. *
  6. 6. * LICENSE: 一些 license 信息
  7. 7. *
  8. 8. * @copyright  Copyright (c) 2012 Company
  9. 9. * @license    BSD License
  10. 10. * @since      File available since Release 1.0
  11. 11.*/  
复制代码
每个类必须至少包含这些phpDocumentor标签:
  1. 1./**
  2. 2. * 类的简述
  3. 3. *
  4. 4. * 类的详细描述 (如果有的话)... ...
  5. 5. *
  6. 6. * @copyright  Copyright (c) 2012 Company
  7. 7. * @license    BSD License
  8. 8. * @since      Class available since Release 1.0
  9. 9. * @deprecated Class deprecated in Release 2.0
  10. 10. */  
复制代码
每个方法必须至少包含这些phpDocumentor标签:
  1. 1./**
  2. 2. * 方法的详细描述
  3. 3. *
  4. 4. * @param   string
  5. 5. * @return  string
  6. 6. */  
复制代码
代码中的单行注释统一使用双斜线"//"方式
  1. 1.// break up the string by newlines  
  2. 2.$parts = explode("\n", $str);  
  3. 3.  
  4. 4.// A longer comment that needs to give greater detail on what is  
  5. 5.// occurring and why can use multiple single-line comments.  Try to  
  6. 6.// keep the width reasonable, around 70 characters is the easiest to  
  7. 7.// read.  Don't hesitate to link to permanent external resources  
  8. 8.// that may provide greater detail:  
  9. 9.//  
  10. 10.// http://example.com/information_about_something/in_particular/  
  11. 11.  
  12. 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

论坛徽章:
0
发表于 2012-02-17 22:06 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP