I initially started searching for a PHP script which would output PHP errors, to the JavaScript console. As the Default behaviour is just to spit them out anywhere and displace everything else.
Although that was not successful, this is the code I have put together so far. Can anyone please review it and say if it can be shortened or optimized? The CSS Class I have added onto the errors just adds position: relative
and a value in my external CSS file.
=> ( Would be better to have position Absolute, but then all the errors overlap regardles of any breaks, n/ lines or PHP_EOL, may get changed to a float I'm not sure yet => They would be better because they don't take up any physical space or cause displacement).
Sure this Can be shortened or optimized somehow. But PHP is not my strong point & struggling.
Are there any security issues with using this? Are they any errors in my Arrays?
<?php
// ::=> ErrorHandler.php [File];
class MyError
{
protected static $collected = array();
public static function getCollectedErrors()
{
return self::$collected;
}
protected static function addError($key, $error)
{
if (!isset(self::$collected[$key]))
self::$collected[$key] = array();
self::$collected[$key][] = $error;
}
// CATCHABLE ERRORS
public static function captureNormal( $number, $message,
$file, $line )
{
// Insert all in one table
$error = array( 'type' => $number,
'message' => $message,
'file' => $file,
'line' => $line
);
// Display content $error variable
self::addError('error', $message . " at " . $file .
':' . $line);
}
public static function captureException( $exception )
{
// Display content $exception variable
self::addError('exception', $exception);
}
// UNCATCHABLE ERRORS
public static function captureShutdown( )
{
$error = error_get_last( );
if( $error ) {
## IF YOU WANT TO CLEAR ALL BUFFER, UNCOMMENT NEXT LINE:
# ob_end_clean( );
// Display content $error variable
self::addError('shutdown', $error);
} else { self::addError('shutdown', '<none>');
return true; }
}
}
set_error_handler(array( 'MyError', 'captureNormal' ));
set_exception_handler(array( 'MyError', 'captureException' ));
register_shutdown_function(array( 'MyError', 'captureShutdown' ));
?>
& in my index & sub-pages =>
<?php
// >>>>>>> Add if Admin Statement in Here & Password Match & Cookie, csrf check.
$errors = MyError::getCollectedErrors(); // This is Linked to ErrorHandlers.php
foreach ($errors as $category => $items) {
echo "<strong class=\"dispLow\"
style=\"display:block;
position:relative;
max-width:248px;
text-wrap:wrap;
z-index:2000;
background:yellow;\">" . $category . ":
</strong><br/>"; // I added class dispLow to the outputted errors with \ escape...
foreach ($items as $error) {
echo "<br>Error:" . $error . $catergory .
$items . $file .
$line . "<br/>" .PHP_EOL;
}
}
?>
Related Questions I could find: