'Error', E_WARNING => 'Warning', E_PARSE => 'Parsing Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'User Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Runtime Notice', E_RECOVERABLE_ERROR => 'Catchable Fatal Error' ); /** * 单例对象 */ protected static $instance; /** * 初始化 * @access public */ public static function hanndle(){ if (is_null(self::$instance)) { self::$instance = new static(); } return self::$instance; } /** * 构造函数 * CPSError constructor. */ private function __construct(){ error_reporting(E_ALL); @register_shutdown_function([$this,'hanndleFatal']); @set_error_handler([$this, 'writeLogAndThrowExcption']); } /** * 错误处理 * @throws \Exception */ public function hanndleFatal(){ $error = error_get_last(); if($error && ($error["type"]===($error["type"] & CPS_FATAL))) { $this->writeLogAndThrowExcption($error["type"], $error["message"], $error["file"], $error["line"]); } } /** * 写日志信息 * @param $errno * @param $errmsg * @param $filename * @param $linenum * @throws \Exception */ public function writeLogAndThrowExcption($errno, $errmsg, $filename, $linenum) { Log::error('CPSException: Type:'.($this->errCode[$errno] ?? '未知错误').' Line:'.$linenum.' File:'.$filename.' Msg:'.$errmsg); throw new \Exception($errmsg); } public function __destruct(){ restore_error_handler(); } }