首页 > 代码库 > 关于全局错误,异常捕获

关于全局错误,异常捕获

class Capture
{
    private static $callback;

    public static function register($callback)
    {
        self::$callback = $callback;
        set_error_handler([__CLASS__, errorHandle], E_ALL ^ E_DEPRECATED ^ E_STRICT ^ E_NOTICE ^ E_WARNING);
        set_exception_handler([__CLASS__, exceptionHandle]);
        register_shutdown_function([__CLASS__, fatalHandle]);
    }

    public static function errorHandle($errno, $errstr, $errfile, $errline)
    {
        call_user_func_array(self::$callback, [file=>$errfile , line => $errline , message => $errstr, type => user_error, level => $errno]);
    }

    public static function exceptionHandle($exception)
    {
        call_user_func_array(self::$callback, [file=>$exception->getFile(), line => $exception->getLine(), message => $exception->getMessage(), type => exception, level => ‘‘]);
    }

    public static function fatalHandle()
    {
        $error = error_get_last ();
        // 捕捉对进程影响的错误信息,并给用户返回正确的页面
        if (in_array ( $error[type], array (
            E_ERROR,
            E_CORE_ERROR,
            E_PARSE,
            E_USER_ERROR,
            E_COMPILE_ERROR,
            E_RECOVERABLE_ERROR,
        ) )) {
            call_user_func_array(self::$callback, [file => $error[file], line => $error[line], message => $error[message], type => error, level => $error[type]]);
        }
    }
}

 

关于全局错误,异常捕获