4

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.

1
  • This really should be in official PHP help, marked as the best practices! Commented Sep 11, 2018 at 6:09

2 Answers 2

9

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'));
Sign up to request clarification or add additional context in comments.

3 Comments

It's worth mentioning, in general, when you want to pass a function name to something and the function is actually an object method, you use the syntax shown here. That is to say, this solution works for other PHP methods expecting a function name.
I saw my error a head of time, my concern is that if the user is setting up the application this is yet another class they have to call as a "new" object.
I know this is for long time ago but it is better to use static::class instead of just the name of the class for the static methods. By using that you can support namespacing. set_exception_handler(array(static::class, 'exception'));
9

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.