Skip to content

Instantly share code, notes, and snippets.

@ZanderBrown
Created March 8, 2018 13:27
Show Gist options
  • Save ZanderBrown/fcd202876e6cb83804f714fb0d7060e8 to your computer and use it in GitHub Desktop.
Save ZanderBrown/fcd202876e6cb83804f714fb0d7060e8 to your computer and use it in GitHub Desktop.
PHP shutdown function useful for debugging
register_shutdown_function(function() {
$e = error_get_last();
if($e !== null) {
gc_mem_caches();
$str = 'E(' . $e['type'] . '): ' . $e['message'] . PHP_EOL;
$line = 1;
$pad = strlen((string) $e['line'] + 2) + 3;
$fh = fopen($e['file'], 'r');
while (($buffer = fgets($fh)) !== FALSE) {
if ($line >= $e['line'] - 2 && $line <= $e['line'] + 2) {
if ($line == $e['line']) {
$str .= str_pad($line . ' ->', $pad);
} else {
$str .= str_pad($line . '.', $pad);
}
$str .= $buffer;
}
$line++;
}
fclose($fh);
$str .= $e['file'];
echo 'PHP died:' . PHP_EOL . $str;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment