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:
- Automatically called when an exception is thrown to deal with the "uncaught" exception.
- Done in an OOP way that is extendible.
The way I have shown is the only way I can think to do it.