tags:

views:

112

answers:

5

I'd like to be able to catch die() and exit() messages. Is this possible? I'm hoping for something similar to set_error_handler and set_exception_handler. I've looked at register_shutdown_function() but it seems to contain no context for the offending die() and exit() calls.

I realize that die() and exit() are bad ways to handle errors. I am not looking to be told not to do this. :) I am creating a generic system and want to be able to gracefully log exit() and die() if for some reason someone (not me) decides this is a good idea to do.

+3  A: 

According to the PHP manual, shutdown functions should still be notified when die() or exit() is called.

Shutdown functions and object destructors will always be executed even if exit() is called.

It doesn't seem to be possible to get the status sent in exit($status). Unless you can use output buffering to capture it, but I'm not sure how you'd know when to call ob_start().

Alex King
I guess the OP's problem is that he would like to be able to retrieve the error message from the shutdown function.
casablanca
The shutdown function is being called, but I am not sure if/how I can get the exit/die context at that point. For that matter, it wasn't clear if I'd even know if exit/die is the reason for the shutdown function being called. (it looked like the shutdown function gets called on die, but it gets called when the process terminates after a successful run as well...)
Beau Simensen
+1  A: 

Maybe override_function() could be interesting, if APD is available

Dr.Molle
This is an interesting idea! Hoping for a better non-APD solution, though.
Beau Simensen
A: 

yes: write a function and use that instead.

function kill($msg){
    // Do your logging..
    exit($msg);
}
JamesM
I appreciate the response, but this is not helpful at all. I want to catch exit() and die() in the event that some code I have not written calls them.
Beau Simensen
i know but that is the closest you can get to it, just one new function and a simple search and replace from exit and die to kill..
JamesM
A: 

Why do not use custom error handling instead? If not, you could always use LD_PRELOAD and C Code injection to catch it :) Or recompile php with your customizations :P

flaab
A: 

As best as I can tell this is not really possible. Some of the solutions posted here may work but they require a lot of additional work or many dependencies. There is no way to easily and reliable trap the die() and exit() messages.

Beau Simensen