在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.

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'));

Everybody thinks that using set_exception_handler would catch all the error in the PHP but it is not true as some errors are not handled by the set_exception_handler the proper way to handle all types of errors have to be done as:

 //Setting for the PHP Error Handler
 set_error_handler( call_back function or class );

 //Setting for the PHP Exceptions Error Handler
 set_exception_handler(call_back function or class);

 //Setting for the PHP Fatal Error
 register_shutdown_function(call_back function or class);

By setting these three setting you can catch all the errors of PHP.