1.不要使用相對路徑
經常會看到:
1 require_once('../../lib/some_class.php');
該方法有不少缺點:
它首先查找指定的php包含路徑, 而後查找當前目錄.
所以會檢查過多路徑.
若是該腳本被另外一目錄的腳本包含, 它的基本目錄變成了另外一腳本所在的目錄.
另外一問題, 當定時任務運行該腳本, 它的上級目錄可能就不是工做目錄了.
所以最佳選擇是使用絕對路徑:
2 define('ROOT' , '/var/www/project/');
3 require_once(ROOT . '../../lib/some_class.php');
4
5 //rest of the code
咱們定義了一個絕對路徑, 值被寫死了. 咱們還能夠改進它. 路徑 /var/www/project 也可能會改變, 那麼咱們每次都要改變它嗎? 不是的, 咱們能夠使用__FILE__常量, 如:
6 //suppose your script is /var/www/project/index.php
7 //Then __FILE__ will always have that full path.
8
9 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命令, 只要有一個系統函數可用, 這保持了代碼的一致性.