編寫PHP高質量代碼

 

 1.不要使用相對路徑 php

經常會看到: html

require_once('../../lib/some_class.php');  shell

該方法有不少缺點: 數據庫

它首先查找指定的php包含路徑而後查找當前目錄. 服務器

所以會檢查過多路徑. ide

若是該腳本被另外一目錄的腳本包含它的基本目錄變成了另外一腳本所在的目錄. 函數

另外一問題當定時任務運行該腳本它的上級目錄可能就不是工做目錄了. 工具

所以最佳選擇是使用絕對路徑: ui

define('ROOT' , '/var/www/project/');   this

require_once(ROOT . '../../lib/some_class.php');  

       

//rest of the code 

咱們定義了一個絕對路徑值被寫死了咱們還能夠改進它路徑 /var/www/project 也可能會改變那麼咱們每次都要改變它嗎不是的咱們能夠使用__FILE__常量:

//suppose your script is /var/www/project/index.php  

//Then __FILE__ will always have that full path.  

       

define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));  

10 require_once(ROOT . '../../lib/some_class.php');  

11    

12 //rest of the code 

如今不管你移到哪一個目錄如移到一個外網的服務器上代碼無須更改即可正確運行.

2. 不要直接使用 require, include, include_once, required_once

能夠在腳本頭部引入多個文件像類庫工具文件和助手函數等:

13 require_once('lib/Database.php');  

14 require_once('lib/Mail.php');  

15 require_once('helpers/utitlity_functions.php'); 

這種用法至關原始應該更靈活點應編寫個助手函數包含文件例如:

16 function load_class($class_name)  

17 {  

18  //path to the class file  

19  $path = ROOT . '/lib/' . $class_name . '.php');  

20  require_once( $path );  

21 }  

22        

23 load_class('Database');  

24 load_class('Mail'); 

有什麼不同嗎該代碼更具可讀性.

將來你能夠按需擴展該函數:

25 function load_class($class_name)  

26 {  

27   //path to the class file  

28     $path = ROOT . '/lib/' . $class_name . '.php');  

29    

30   if(file_exists($path))  

31     {  

32  require_once( $path );  

33         }  

34 

還可作得更多:

爲一樣文件查找多個目錄

能很容易的改變放置類文件的目錄無須在代碼各處一一修改

可以使用相似的函數加載文件html內容.

3. 爲應用保留調試代碼

在開發環境中咱們打印數據庫查詢語句轉存有問題的變量值而一旦問題解決咱們註釋或刪除它們然而更好的作法是保留調試代碼.

在開發環境中你能夠:

35 define('ENVIRONMENT' , 'development');  

36    

37 if(! $db->query( $query )  

38 {  

39    if(ENVIRONMENT == 'development')  

40    {  

41       echo "$query failed";  

42   }  

43    else  

44     {  

45        echo "Database error. Please contact administrator";  

46     }  

47 

在服務器中你能夠:

48 define('ENVIRONMENT' , 'production');  

49 if(! $db->query( $query )  

50 {  

51    if(ENVIRONMENT == 'development')  

52    {  

53        echo "$query failed";  

54    }  

55     else  

56     {  

57         echo "Database error. Please contact administrator";  

58     }  

59 

4. 使用可跨平臺的函數執行命令

system, exec, passthru, shell_exec 4個函數可用於執行系統命令每一個的行爲都有細微差異問題在於當在共享主機中某些函數可能被選擇性的禁用大多數新手趨於每次首先檢查哪一個函數可用然而再使用它.

更好的方案是封成函數一個可跨平臺的函數。

60 01  /**  

61 02      Method to execute a command in the terminal  

62 03      Uses :  

63 04     

64 05      1. system  

65 06      2. passthru  

66 07      3. exec  

67 08      4. shell_exec  

68 09     

69 10  */  

70 11  function terminal($command)  

71 12  {  

72 13      //system  

73 14      if(function_exists('system'))  

74 15      {  

75 16          ob_start();  

76 17          system($command , $return_var);  

77 18          $output = ob_get_contents();  

78 19          ob_end_clean();  

79 20      }  

80 21      //passthru  

81 22      else if(function_exists('passthru'))  

82 23      {  

83 24          ob_start();  

84 25          passthru($command , $return_var);  

85 26          $output = ob_get_contents();  

86 27          ob_end_clean();  

87 28      }  

88 29     

89 30      //exec  

90 31      else if(function_exists('exec'))  

91 32      {  

92 33          exec($command , $output , $return_var);  

93 34          $output = implode("\n" , $output);  

94 35      }  

95 36     

96 37      //shell_exec  

97 38      else if(function_exists('shell_exec'))  

98 39      {  

99 40          $output = shell_exec($command) ;  

100 41      }  

101 42     

102 43      else  

103 44      {  

104 45          $output = 'Command execution not possible on this system';  

105 46          $return_var = 1;  

106 47      }  

107 48     

108 49      return array('output' => $output , 'status' => $return_var);  

109 50  }  

110 51     

111 52  terminal('ls'); 

上面的函數將運行shell命令只要有一個系統函數可用這保持了代碼的一致性

更全的下載附件查看

相關文章
相關標籤/搜索