下面小编就为大家带来一篇关于php通用返回值设置方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
遇到一个不错的php代码。记录一下。
在写php代码时,经常会遇到需要返回值的情况,可以统一设置一下返回值的格式。
下面就是一个不错的例子。
配置类return.conf.php
<?php
define("return_val", "return array('code' => 0, 'msg' => '', 'data' => '');");
define("return_success", 0);
define("return_runtime_err", 1);
define("return_file_not_exist", 2);
class returnconf{
public static function commonreturn(){
return eval(return_val);
}
}
?>
测试、使用test.php
<?php
require_once("return.conf.php");
function get_file_line($filename){
$result = returnconf::commonreturn();
$cmd = "wc -l $filename | awk '{print $1}'";
exec($cmd, $output, $code);
if (return_success !== $code){
$result['code'] = return_runtime_err;
$result['msg'] = "exec $cmd err";
return $result;
}
$result['data'] = $output[0];
return $result;
}
print_r(get_file_line("test.php"));
?>
以上就是关于php通用返回值设置方法的示例代码分享的详细内容。
