提高PHP代码质量的36个技巧

2019-08-26 18:32

本文来自 Binarytides 博主 Silver Moon 的整理分享。 1.不要使用相对路径 常常会看到:

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

该方法有很多缺点:

它首先查找指定的php包含路径, 然后查找当前目录. 因此会检查过多路径.

如果该脚本被另一目录的脚本包含, 它的基本目录变成了另一脚本所在的目录. 另一问题, 当定时任务运行该脚本, 它的上级目录可能就不是工作目录了. 因此最佳选择是使用绝对路径:

1 2 3 4 view sourceprint?

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

require_once(ROOT . '../../lib/some_class.php'); //rest of the code

我们定义了一个绝对路径, 值被写死了. 我们还可以改进它. 路径 /var/www/project

也可能会改变, 那么我们每次都要改变它吗? 不是的, 我们可以使用__FILE__常量, 如:

1 2 3 4 5

行.

//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_class.php'); //rest of the code

现在, 无论你移到哪个目录, 如移到一个外网的服务器上, 代码无须更改便可正确运2. 不要直接使用 require, include, include_once, required_once 可以在脚本头部引入多个文件, 像类库, 工具文件和助手函数等, 如:

1 require_once('lib/Database.php'); 2 require_once('lib/Mail.php');

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

这种用法相当原始. 应该更灵活点. 应编写个助手函数包含文件. 例如:

1 2 3 4 5 6 7 8 function load_class($class_name) {

//path to the class file

$path = ROOT . '/lib/' . $class_name . '.php'); require_once( $path ); }

load_class('Database'); load_class('Mail');

有什么不一样吗? 该代码更具可读性. 將来你可以按需扩展该函数, 如:

1 2 3 4 function load_class($class_name) {

//path to the class file

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

5 6 7 8 9 if(file_exists($path)) {

require_once( $path ); } }

还可做得更多:

为同样文件查找多个目录

能很容易的改变放置类文件的目录, 无须在代码各处一一修改 可使用类似的函数加载文件, 如html内容. 3. 为应用保留调试代码

在开发环境中, 我们打印数据库查询语句, 转存有问题的变量值, 而一旦问题解决, 我

们注释或删除它们. 然而更好的做法是保留调试代码.

在开发环境中, 你可以:

1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

define('ENVIRONMENT' , 'development'); if(! $db->query( $query ) {

if(ENVIRONMENT == 'development') {

echo \ } else {

echo \ } }

在服务器中, 你可以:

define('ENVIRONMENT' , 'production'); if(! $db->query( $query ) {

if(ENVIRONMENT == 'development') {

echo \ } else {

echo \ } }

4. 使用可跨平台的函数执行命令

system, exec, passthru, shell_exec 这4个函数可用于执行系统命令. 每个的行为都

有细微差别. 问题在于, 当在共享主机中, 某些函数可能被选择性的禁用. 大多数新手趋于每次首先检查哪个函数可用, 然而再使用它.

更好的方案是封成函数一个可跨平台的函数.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 /**

Method to execute a command in the terminal Uses : 1. system 2. passthru 3. exec

4. shell_exec */

function terminal($command) {

//system

if(function_exists('system')) {

ob_start();

system($command , $return_var); $output = ob_get_contents(); ob_end_clean(); }

//passthru

else if(function_exists('passthru')) {

ob_start();

passthru($command , $return_var); $output = ob_get_contents(); ob_end_clean(); }

//exec

else if(function_exists('exec')) {

exec($command , $output , $return_var); $output = implode(\ }

//shell_exec

else 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); }

45 terminal('ls');

上面的函数將运行shell命令, 只要有一个系统函数可用, 这保持了代码的一致性. 5. 灵活编写函数

function add_to_cart($item_id , $qty)

1 2 3 4 {

$_SESSION['cart']['item_id'] = $qty; }

add_to_cart( 'IPHONE3' , 2 );

使用上面的函数添加单个项目. 而当添加项列表的时候,你要创建另一个函数吗? 不用,

只要稍加留意不同类型的参数, 就会更灵活. 如:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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建议的博客文章都没提到这点.

1

2 echo \

3 //Now dont close this tag

这將节约你很多时间. 我们举个例子: 一个 super_class.php 文件

1 2 3 4 5 6 7 8 9

class super_class {

function super_function() {

//super code } } ?>

10 //super extra character after the closing tag

index.php

1 require_once('super_class.php');

2 //echo an image or pdf , or set the cookies or session data

这样, 你將会得到一个 Headers already send error. 为什么? 因为 “super extra character” 已经被输出了. 现在你得开始调试啦. 这会花费大量时间寻找 super extra 的位置.

因此, 养成省略关闭符的习惯:

1 2 3 4 5 6 7 8 9

class super_class {

function super_function() {

//super code } }

//No closing tag

这会更好.

7. 在某地方收集所有输入, 一次输出给浏览器 这称为输出缓冲, 假如说你已在不同的函数输出内容:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 function print_header() {

echo \ }

function print_footer() {

echo \ }

print_header();

for($i = 0 ; $i < 100; $i++) {

echo \ }

print_footer();

替代方案, 在某地方集中收集输出. 你可以存储在函数的局部变量中, 也可以使用

ob_start和ob_end_clean. 如下:

1 2 3 4 5 6 7 8 function print_header() {

$o = \ return $o; }

function print_footer() {

$o = \


提高PHP代码质量的36个技巧.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:湖南省衡阳市第八中学2018届高三(实验班)第三次质检地理试题(

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: