Ho provato a scrivere questa funzione... che ne dite?
Codice PHP:
function default_handler ($message, $error, $file, $line) {
$errors = [
E_ERROR => "Fatal error",
E_USER_ERROR => "Fatal error",
E_WARNING => "Warning",
E_USER_WARNING => "Warning",
E_NOTICE => "Notice",
E_USER_NOTICE => "Notice",
];
echo "<br/><b>" . $errors[$error] . "</b>: $message in <b>$file</b> on line <b>$line</b><br/>";
}
function throw_error ($message, $error = E_USER_NOTICE, $file = null, $line = null) {
$trace = debug_backtrace()[1];
$file = is_null($file) ? $trace["file"] : $file;
$line = is_null($line) ? $trace["line"] : $line;
$old_handler = set_error_handler(null);
if (is_null($old_handler)
|| !call_user_func($old_handler, $error, $message, $file, $line)
) {
default_handler ($message, $error, $file, $line);
}
restore_error_handler();
if (in_array($error, [E_ERROR, E_USER_ERROR])) {
exit();
}
}
(In realtà le funzioni le ho all'interno della mia classe per ora)
Funziona così:
Codice PHP:
throw_error("Messaggio", E_WARNING, "index.php", 89);
Se non viene fornita la pagina o la linea prende quelle in cui è richiamata la funzione che contiene throw_error(...)
E 'sta roba funziona anche se viene modificato l'error handler con set_error_handler()