关于全局错误,异常捕获
Posted tianye_guazi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于全局错误,异常捕获相关的知识,希望对你有一定的参考价值。
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‘]]); } } }
以上是关于关于全局错误,异常捕获的主要内容,如果未能解决你的问题,请参考以下文章