require_once('../../lib/some_>
该方法有很多缺陷:
它首先查找指定的php包含路径, 然后查找当前目录.
因此会检讨过多路径.

如果该脚本被另一目录的脚本包含, 它的基本目录变成了另一脚本所在的目录.
另一问题, 当定时任务运行该脚本, 它的上级目录可能就不是事情目录了.
因此最佳选择是利用绝对路径:
define('ROOT' , '/var/www/project/');require_once(ROOT . '../../lib/some_>
我们定义了一个绝对路径, 值被写去世了. 我们还可以改进它. 路径 /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));require_once(ROOT . '../../lib/some_>
现在, 无论你移到哪个目录, 如移到一个外网的做事器上, 代码无须变动便可精确运行.
2. 不要直策应用 require, include, include_once, required_once可以在脚本头部引入多个文件, 像类库, 工具文件和助手函数等, 如:
require_once('lib/Database.php');require_once('lib/Mail.php');require_once('helpers/utitlity_functions.php');
这种用法相称原始. 该当更灵巧点. 应编写个助手函数包含文件. 例如:
function load_>
有什么不一样吗? 该代码更具可读性.
將来你可以按需扩展该函数, 如:
function load_>
还可做得更多:
为同样文件查找多个目录
能很随意马虎的改变放置类文件的目录, 无须在代码各处逐一修正
可利用类似的函数加载文件, 如html内容.
3. 为运用保留调试代码在开拓环境中, 我们打印数据库查询语句, 转存有问题的变量值, 而一旦问题办理, 我们注释或删除它们. 然而更好的做法是保留调试代码.
在开拓环境中, 你可以:
define('ENVIRONMENT' , 'development');if(! $db->query( $query ){ if(ENVIRONMENT == 'development') { echo \"大众$query failed\"大众; } else { echo \"大众Database error. Please contact administrator\"大众; }}
在做事器中, 你可以:
define('ENVIRONMENT' , 'production');if(! $db->query( $query ){ if(ENVIRONMENT == 'development') { echo \"大众$query failed\公众; } else { echo \"大众Database error. Please contact administrator\"大众; }}4. 利用可跨平台的函数实行命令
system, exec, passthru, shell_exec 这4个函数可用于实行系统命令. 每个的行为都有细微差别. 问题在于, 当在共享主机中, 某些函数可能当选择性的禁用. 大多数新手趋于每次首先检讨哪个函数可用, 然而再利用它.
更好的方案是封成函数一个可跨平台的函数.
/Method to execute a command in the terminalUses :1. system2. passthru3. exec4. shell_exec/function terminal($command){//systemif(function_exists('system')){ob_start;system($command , $return_var);$output = ob_get_contents;ob_end_clean;}//passthruelse if(function_exists('passthru')){ob_start;passthru($command , $return_var);$output = ob_get_contents;ob_end_clean;}//execelse if(function_exists('exec')){exec($command , $output , $return_var);$output = implode(\"大众n\公众 , $output);}//shell_execelse if(function_exists('shell_exec')){$output = shell_exec($command) ;}else{$output = 'Command execution not possible on this system';$return_var = 1;}return array('output' => $output , 'status' => $return_var);}terminal('ls');
上面的函数將运行shell命令, 只要有一个别系函数可用, 这保持了代码的同等性.
5. 灵巧编写函数function add_to_cart($item_id , $qty){ $_SESSION['cart']['item_id'] = $qty;}add_to_cart( 'IPHONE3' , 2 );
利用上面的函数添加单个项目. 而当添加项列表的时候,你要创建另一个函数吗? 不用, 只要稍加留神不同类型的参数, 就会更灵巧. 如:
function add_to_cart($item_id , $qty){ if(!is_array($item_id)) { $_SESSION['cart']['item_id'] = $qty; } else { foreach($item_id as $i_id => $qty) { $_SESSION['cart']['i_id'] = $qty; } }}add_to_cart( 'IPHONE3' , 2 );add_to_cart( array('IPHONE3' => 2 , 'IPAD' => 5) );
现在, 同个函数可以处理不同类型的输入参数了. 可以参照上面的例子重构你的多处代码, 使其更智能.
6. 故意忽略php关闭标签我很想知道为什么这么多关于php建议的博客文章都没提到这点.
这將节约你很多韶光. 我们举个例子:
一个 super_>
//super extra character after the closing tag
index.php
require_once('super_>
这样, 你將会得到一个 Headers already send error. 为什么? 由于 “super extra character” 已经被输出了. 现在你得开始调试啦. 这会花费大量韶光探求 super extra 的位置.
因此, 养成省略关闭符的习气: