Asking for decent thoughts about this:
I'd like to implement some mechanism in PHP code that can run any external code and calls a callback function if one of the inclusions fails therein (include
, require
+ *_once
).
External code means that the code that is getting executed is not written by me nor is there control over it. It's included for testing. So having detailed info about inclusions failures deeper therein is helpful.
I'm runnning into the problem that it looks impossible to have a callback when a PHP fatal error happens.
What I tried so far:
- Registering an error handler via
set_error_handler
- Does not work with fatal errors. - Created an object instance with a
__destruct()
method - Is not invoked with fatal errors. - Registered a shutdown function - Is not called on fatal errors.
In any of these I just wanted to fetch a debug_backtrace
and then work with the information given.
So question shortly is: how to track failed file inclusions from within PHP code and call a function then.
I fear the answer to the question is no from my recent tries and searches, so anything insightful is highly appreciated. Even if your answer only strengthens the "not possible" point.
Additionally it's helpful as well if it's possible to find out which file is going to be included, so creating a debug output before the inclusion (failing or not) could be done at least.
Remarks:
- Preferable w/o extensions. However if something exists, I'm eager to know as well.
- External code means that the code that is getting executed is not written by me nor is there control over it. It's included for testing. So having detailed info about inclusions failures deeper therein is helpful.
Related:
- How can I get PHP to produce a backtrace upon errors?
- set_error_handler() doens't work for FATAL error (
register_shutdown_function
+error_get_last
)