I’m currently using register_shutdown_function() for multiple purposes. One use is for handling fatal errors, while the other is for logging resources used during execution like time, memoryusage etc.
Currently I register two different shutdown functions, but on one test only the first ran while the other seemed to fail. Now this could ofcourse be triggered by some error within the function itself so I have rewritten it, but is it possible an error was caused by using several register_shutdown_function calls? So what is considered best practice here, to registert two different functions or just make the call to one function that handles the different tasks?
Is it also safe (and possible) to make the function load a class for errorhandling if a fatal error occurs or should I keep the functionality within the function itself?
The final question I got is if there’s a better way to handle fatal errors than using shutdown functions? I tried using set_error_handler, but it does not cover all errortypes so some errors will not trigger this.
Hope these questions are well formulated and clear. My goal is to keep the code as solid as possible and I could not find any decent answers to the questions I had.
*Edit: Found the answer to my first question, registering several functions should be no problem so the error had to be within the function itself. Leaving the question up to get answers to whether there’s a better ways to handle fatal errors.
IIRC, if you have multiple shutdown functions registered, they will be executed in the order in which they were registered; and you should never have an exit statement in any, otherwise subsequent shutdown functions will not be run. That means you need to take great care if you have multiple functions rather than a single shutdown function.
However, if you’re passing different arguments to the different functions, you should ensure that you have default values for them all in case the functions are called (perhaps triggered by an error) before all the appropriate variables are set.
Personally, I register multiple functions, for similar purposes to yourself; but I’m very careful about the logic within them, and the order of registration.
It’s also not a good idea to use includes or similar in shutdown functions (especially where one is an exception handler), in case the include itself triggers an exception