zoukankan      html  css  js  c++  java
  • php实现实例化类后自动进行错误以及异常处理(简易版)

    <?php
     
    class App
    {
        public function __construct()
        {
            /*
             * ini_set  设置配置项
             * display_errors  是否在页面显示错误信息
             */
            ini_set('display_errors', 0);
            $this->setSysHandler();
        }
     
        public function setSysHandler()
        {
            //php中止时执行
            register_shutdown_function([$this, 'fatalHandler']);
            //设置用户自定义的错误处理函数
            set_error_handler([$this, 'errorHandler']);
            //设置用户自定义的异常处理函数
            set_exception_handler([$this, 'exceptionHandler']);
        }
     
        // 错误被包装成为异常抛出
        public function errorHandler($code, $msg, $file, $line)
        {
            throw new ErrorException($msg, $code, $code, $file, $line);
        }
     
     
        public function fatalHandler()
        {
            if ($errors = error_get_last()) {
                $msg = $errors['message'];
                $code = $errors['type'];
                $file = $errors['file'];
                $line = $errors['line'];
                echo "<font size='7'>:(</font><h2> 文件: {$file};   行号: {$line};</h2>";
                echo "<h4>错误信息: {$msg}; 错误代码: {$code}</h4>";
                echo "<pre>";
            }
        }
     
        public function exceptionHandler($excep)
        {
            $this->handler($excep);
        }
     
        public function handler($excep)
        {
            $msg = $excep->getMessage();//获取异常消息内容
            $code = $excep->getCode();//获取异常代码
            $file = $excep->getFile();//创建异常时的程序文件名称
            $line = $excep->getLine();//获取创建的异常所在文件中的行号
            $trace = $excep->getTrace();//获取异常追踪信息
            $this->errorlog($msg, $code, $file, $line);//发送错误信息到某个地方
            echo "<font size='7'>:(</font><h2> 文件: {$file};   行号: {$line};</h2>";
            echo "<h4>错误信息: {$msg}; 错误代码: {$code}</h4>";
            echo "<pre>";
            if ($excep instanceof ErrorException) {
                array_shift($trace);
            }
            print_r($trace);
            //函数的调用栈
        }
     
        public function errorlog($msg, $code, $file, $line)
        {
            $str = date('Y-m-d H:i:s') . "
    ";
            $str .= "错误信息是:";
            $str .= $msg;
            $str .= "
    ";
            $str .= "错误行号是:";
            $str .= $line;
            $str .= "
    ";
            $str .= "错误代码是:";
            $str .= $code;
            $str .= "
    ";
            $str .= "错误行文件:";
            $str .= $file;
            $str .= "
    ";
            $str .= "
    ";
            error_log($str, 3, './myerror.log');
        }
    }
     
    $app = new App();

    原文链接:https://blog.csdn.net/qq_20025577/article/details/85059762

  • 相关阅读:
    AI人脸识别SDK接入 — 参数优化篇(虹软)
    虹软人脸识别ArcFace2.0 Android SDK使用教程
    虹软免费人脸识别SDK注册指南
    python3+arcface2.0 离线人脸识别 demo
    Android利用RecyclerView实现列表倒计时效果
    将博客搬至CSDN
    Retrofit动态设置支持JSON和XML格式转换工厂
    Android 简单统计文本文件字符数、单词数、行数Demo
    [雨]个人项目设计分析
    Android:随机生成算数四则运算简单demo(随机生成2~4组数字,进行加减乘除运算)
  • 原文地址:https://www.cnblogs.com/init-007/p/11312035.html
Copyright © 2011-2022 走看看