亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1514 | 回復(fù): 1
打印 上一主題 下一主題

PHP代碼樣式 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-02-17 14:20 |只看該作者 |倒序瀏覽
PHP代碼樣式







phpcoding style
如果使用第三方框架,并且有提供代碼樣式說明,則遵循其代碼樣式標準,否則如下!

文件格式
文件統(tǒng)一使用UTF-8編碼,LF(Unix)換行符。

文件名統(tǒng)一采用小寫字母+下劃線格式,如果是PHP類文件,文件名應(yīng)與類名相同(如:文件名為form_validation.php,相對應(yīng)的類為"FormValidation")。

代碼格式
代碼必須使用標準的PHP標簽定界,禁止使用短標簽(<? //... ?>),對于只包含有PHP代碼的文件,禁止使用PHP結(jié)束標志("?>"),文件末尾使用注釋說明"/* End of file <filename.php> */"。這樣做,可以防止它的末尾的被意外地注入相應(yīng)。
  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 */
復(fù)制代碼
代碼縮進采用4空格,禁止使用Tab作為代碼縮進,單行長度為80個字符,最大不要超過120個字符。

字符串中沒有變量的統(tǒng)一采用單引號括起來,對于含有變量的字符串,變量應(yīng)寫做
  1. // 錯誤   
  2. $greeting = "Welcome ${name}!";   
  3. // 正確   
  4. $greeting = "Welcome {$name}!";  

  5. // 錯誤
  6. $greeting = "Welcome ${name}!";
  7. // 正確
  8. $greeting = "Welcome {$name}!";
復(fù)制代碼
類和方法
類名和方法名均使用駝峰式命名法,只是類名首字母大寫,方法名首字母小寫,命名應(yīng)該保持意思精準短小。
  1. // 錯誤   
  2. class superclass   
  3. class superClass   
  4. // 正確   
  5.   
  6. class SuperClass  

  7. // 錯誤
  8. class superclass
  9. class superClass
  10. // 正確

  11. class SuperClass
復(fù)制代碼
  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()
復(fù)制代碼
變量
變量名統(tǒng)一使用小寫字母,單詞之間用下劃線分割,也應(yīng)該保持意思精準短小,禁止使用臃腫的變量名,亦禁止使用單字符做為局部(或全局)變量(如$i),在for循環(huán)中除外。
  1. // 錯誤   
  2. $i = "foobar";  // 單字符變量只充許使用在for循環(huán)中   
  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循環(huán)中
  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
復(fù)制代碼
常量
常量名統(tǒng)一采用大寫字母與下劃線方式,在命名時也應(yīng)該保持意思精準短小,也應(yīng)該避免使用單字符命名。
  1. // 錯誤   
  2. MyConstant       // 應(yīng)該用下劃線并且字母沒有全大寫   
  3. N                     // 單字符   
  4. S_C_VER           // 意思不清楚   
  5. // 正確   
  6. MY_CONSTANT   
  7. NEWLINE   
  8. SUPER_CLASS_VERSION  

  9. // 錯誤
  10. MyConstant       // 應(yīng)該用下劃線并且字母沒有全大寫
  11. N                     // 單字符
  12. S_C_VER           // 意思不清楚
  13. // 正確
  14. MY_CONSTANT
  15. NEWLINE
  16. SUPER_CLASS_VERSION
復(fù)制代碼
注釋
所有文檔塊 必須和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. */
復(fù)制代碼
每個類必須至少包含這些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. */
復(fù)制代碼
每個方法必須至少包含這些phpDocumentor標簽:
  1. /**  
  2. * 方法的詳細描述  
  3. *  
  4. * @param   string  
  5. * @return  string  
  6. */  

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


  13. 代碼中的單行注釋統(tǒng)一使用雙斜線"//"方式
復(fù)制代碼
  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
復(fù)制代碼
其他
Javascript 代碼樣式參見(英文) http://google-styleguide.googlec ... javascriptguide.xml

最后更新 2012/2/17

論壇徽章:
0
2 [報告]
發(fā)表于 2012-02-17 22:08 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP