I initially started searching for a php script which would output php errors, to the javascript console. As the Default behavious 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 Add's 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
n/ PHP_EOL, may get change to a float i'm not sure yet ).
Sure this Can be shortened or optimized somehow. But Php is not my strong point & struggleing.
is there any security issues with using this.
<?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 ) // Document Name :: errorHandler.php //
{
// 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; } // Document Name :: errorHandler.php //
}
}
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 . $items . $catergory ."<br/>" .PHP_EOL;
}
}
?>
```