Register_shutdown_function () - What is best practice?

I am currently using the register_shutdown_function () function for several purposes. One use is for handling fatal errors, while the other is for logging resources used at runtime, such as time, memory, etc.

I am currently registering two different shutdown functions, but on one test only the first ran, and the other seems to have failed. Now this may be caused by some error inside the function itself, so I rewrote it, but is it possible that the error is caused by using several calls to register_shutdown_function? So, what is considered the best practice here, for registering two different functions or just for calling one function that handles different tasks?

Is it also safe (and possible) for a function to load a class to handle errors if a fatal error occurs, or should I keep the functionality inside the function itself?

The last question I got is a better way to deal with fatal errors than using the shutdown features? I tried using set_error_handler, but it does not cover all errors, so some errors will not trigger this.

I hope that these questions are well formulated and understandable. My goal is to keep the code as solid as possible, and I could not find decent answers to the questions that I had.

* Edit: I found the answer to my first question, registering several functions should not be a problem, so the error should be in the function itself. Leaving the question to get answers to the question, are there more efficient ways to handle fatal errors.

+4
source share
1 answer

IIRC, if you have several shutdown functions, they will be executed in the order in which they were registered; and you should never have an exit statement anyway, otherwise the subsequent shutdown functions will not be executed. This means that you need to be very careful if you have more than one function, and not just one shutdown function.

However, if you pass different arguments to different functions, you must make sure that you have default values ​​for all of them in the case of calling functions (possibly caused by an error) before all the corresponding variables are set.

Personally, I am registering several functions for similar purposes; but I am very careful in the logic inside them and the order of registration.

It is also not recommended to use included or similar shutdown functions (especially if one of them is an exception handler) if include itself throws an exception

+2
source

Source: https://habr.com/ru/post/1335078/


All Articles