PHP捕获异常register_shutdown_function和error_…

2019-07-23 08:41:39来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 

register_shutdown_function

注册一个会在php中止时执行的函数,注册一个 callback ,它会在脚本执行完成或者 exit() 后被调用。

 

error_get_last

获取最后发生的错误,包含type(错误类型),message(错误消息),file(发生错误所在的文件),line(发生错误所在的行)的一个数组,如果没有错误则返回null。

 

两个函数可以结合使用,获取程序发生的错误,并记录日志信息。

以下是个简单例子:

class errors
{
    /**
     *  回调函数
     */
    function shutdown()
    {
        // 获取错误
        $error = error_get_last();
        if ($error) {
            // 记录日志信息
            var_dump($error);
        }
    }
}
 
class test{
    function test_shutdown()
    {
        // 注册一个会在php中止时执行的函数 shutdown
        register_shutdown_function([new errors(), 'shutdown']);
        // 这里调用一个不存在的函数测试
        testaa();
    }
}
$test = new test();
$test->test_shutdown();

 


原文链接:https://www.cnblogs.com/woods1815/p/11145464.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【php socket通讯】php实现http服务

下一篇:ThinkPHP框架