douhuangjian9627 2013-02-19 05:11
浏览 51
已采纳

在php中创建一个错误处理程序类

I recently have been asking question on exceptions and handling exceptions and such, which as recently best explained to me in this question. My question now is how would I use the

set_exception_handler();

in a class to set up a error handling class in php, that when ever an error is thrown is handled by this class. As the definition states:

Sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

I was thinking I could do something like:

class Test{
    public function __construct(){
        set_exception_handler('exception');
    }

    public function exception($exception){
        echo $exception->getMessage();
    }
}

But then the problem is that if the user is setting up the application or using any API from the application they have to do a whole: new Test();

So how could I write an exception handler class that is:

  1. Automatically called when an exception is thrown to deal with the "uncaught" exception.
  2. Done in an OOP way that is extendible.

The way I have shown is the only way I can think to do it.

  • 写回答

2条回答 默认 最新

  • donglingsai2880 2013-02-19 05:18
    关注

    For your class to work, the line inside your contructor should be:

    // if you want a normal method        
    set_exception_handler(array($this, 'exception'));
    
    // if you want a static method (add "static" to your handler method
    set_exception_handler(array('Test', 'exception'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料