ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。
<?php
//--框架核心--Start
//框架内置错误处理
function errDealWith($errno, $errstr, $errfile, $errline)
{
throw new Exception('错误信息:' . $errstr . '错误文件:' . $errfile . '错误行:' . $errline . '错误码:' . $errno);
}
set_error_handler('errDealWith');
//加载框架
try
{
init();
}
catch (Exception $e)
{
showDebug($e);
//showDebug($e->getMessage());
}
//框架调用栈方法
function showDebug($ex)
{
//错误输出
var_dump($ex->getMessage());
//调用栈输出
var_dump('调用顺序');
$traces = array_reverse($ex->getTrace());
foreach ($traces as $item)
{
//剔除框架自带的异常函数
if ($item['function'] != 'errDealWith')
{
var_dump($item['function']);
var_dump($item);
}
}
}
//框架初始化方法
function init()
{
//加载index控制器
index();
}
//--框架核心--结束
//-----------------------华丽的分割线
//控制器
function index()
{
//加载新闻的逻辑
getList();
}
//新闻页面
function getList()
{
return $list;
}在上面的代码中存在$list未定义的错误。首先要自定义错误处理函数或类,这里为了演示只是处理了一种错误而已,实际上一个完整的错误处理类是非常庞大的,通过抛出异常再获取getTrace即可实现,开发自己的框架也可以采用。
项目需要使用websocket推送最新订单,客户服务器非linux不支持swoole,因此使用原生,直接上代码(1).PHP服务端<?php ini_set('error_reporting', E_ALL ^ E_NOTICE); ini_set...
如果想在windows中执行php,并且让php脚本在后台运行,可以用下面的cmd命令start /b php D:\wwwroot\default\demo1\run.php例如上面的命令意思后台运行run.php,如果想用php编写异步代码: ...
面试中PHP面试官会问调用一个不存在的方法,如何知道是哪个文件哪行调用的?假设方法是getWorkLoad()回答1:开启PHP错误输出,PHP会输出Fatal error: Call to undefined function getWorkLoad() in D:\wwwroot\thinkpa...
<?php $member = new class { public function getInfo() { ...
<?php //如果支持exec函数,可以使用的方式 exec('chcp 65001'); //如果exec函数因安全问题禁用,可以使用的方式 pclose(popen('chcp 65001', 'r'));...
将jsonp转为PHP数组和对象。/** * jsonp转数组|Jsonp转json * @param string $jsonp jsonp字符串 * @param bool $as...