Skip to content

Instantly share code, notes, and snippets.

@lyoshenka
Last active August 29, 2015 14:17
Show Gist options
  • Save lyoshenka/d64ff48b56331518e3b7 to your computer and use it in GitHub Desktop.
Save lyoshenka/d64ff48b56331518e3b7 to your computer and use it in GitHub Desktop.
Strict mode for PHP. Show all errors, throw exceptions for errors, etc.
ini_set('display_errors', 'on');
error_reporting(E_ALL);
set_error_handler(function ($type, $message, $file = null, $line = null, $context = null) {
// dont throw error if it might have been supressed with an @
if ($file !== null && $line !== null)
{
$errorLine = explode("\n", file_get_contents($file))[$line-1];
if (strpos($errorLine, '@') !== false && !preg_match('/@[a-z0-9-_.]+\.[a-z]{2,6}/i', $errorLine))
{
return;
}
}
$errorConstants = [
'E_ERROR','E_WARNING','E_PARSE','E_NOTICE','E_CORE_ERROR','E_CORE_WARNING',
'E_COMPILE_ERROR','E_COMPILE_WARNING','E_USER_ERROR','E_USER_WARNING','E_USER_NOTICE',
'E_STRICT','E_RECOVERABLE_ERROR','E_DEPRECATED','E_USER_DEPRECATED','E_ALL'
];
$errorName = 'E_UNKNOWN('.$type.')';
foreach($errorConstants as $constant) {
if ($type == constant($constant)) {
$errorName = $constant;
break;
}
}
throw new ErrorException($errorName.': '.$message, $type, 0, $file, $line);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment