zoukankan      html  css  js  c++  java
  • PHP捕获异常register_shutdown_function和error_get_last的使用

    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();
  • 相关阅读:
    第一个gulp程序
    r.js打包
    吃饭途中的回忆
    IE下script标签的readyState属性
    CSS 选择器
    html的base标签
    迷你MVVM框架 avalonjs 1.3.9发布
    2014年的年终总结
    Visual Studio2017 数据库架构比较
    MVC开发中自定义返回类型
  • 原文地址:https://www.cnblogs.com/woods1815/p/11145464.html
Copyright © 2011-2022 走看看