How to find the line on which execution was interrupted in PHP
When working with old code, it is sometimes very difficult to find the place where execution was interrupted. Constructions like die, exit, etc. are very bad practice, but there is a lot of legacy code (and not only - I'm not saying anything bad about Wordpress, so I'll keep quiet) that abuse them. One of the biggest problems with such constructions is that they are difficult to find and debug. (Use exceptions!)
But when the task is to find the place where code execution is interrupted, you can use the following snippet:
<?php
function shutdown_find_exit()
{
file_put_contents(
__DIR__ . '/tmp/' . uniqid('', true) . '.dump',
var_export($GLOBALS['dbg_stack'], true)
);
}
register_shutdown_function('shutdown_find_exit');
function write_dbg_stack()
{
$GLOBALS['dbg_stack'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
register_tick_function('write_dbg_stack');
declare(ticks=1);
This code should be placed at the beginning of the front-controller (or script). On each tick, it writes the backtrace, and in case of execution interruption, it writes everything to the file tmp/uniq_name (if necessary, change the name), which can be analyzed later.