Created
March 8, 2018 13:27
-
-
Save ZanderBrown/fcd202876e6cb83804f714fb0d7060e8 to your computer and use it in GitHub Desktop.
PHP shutdown function useful for debugging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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