2
\$\begingroup\$

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:

>> Php Error Handler/Logger
>> Echo to Console

\$\endgroup\$
1
\$\begingroup\$

Fixing the white space alone will improve readability.

Use private variables instead of protected when you don't plan to use the variable in an extended class.

Use curly braces even when the executed code is only one line long.

If you want to display the errors in a console, you can use console.log inside of a <script>. Such as this:

$errorMessage;

foreach ($items as $error) 
{
    $errorMessage .= "Error:" . $error . $catergory . 
                            $items . $file .
                            $line . "\n";
}

echo "<script> console.log('" . $errorMessage . "'); </script>";
\$\endgroup\$
0
\$\begingroup\$

First off I must say that the indentation is quite inconsistent and makes reading this code challenging. While most of the inconsistencies appear to be indentation on braces, the indentation on comments also seems a bit wonky. While I don't adhere to everything in it, I suggest following PSR-2 - especially using 4 spaces for indentation.

One thing I observed in the sample usage code is that $file and $line are NULL. Perhaps you were intending to use those from the array $error. I also noticed captureNormal() stores an array in the variable $error but that doesn't appear to be used after it is declared (despite the comment on the next line: Display content $error variable). Maybe you took the usage of $error out of that static method and moved it into the other file?

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);
                         }

To avoid the need to escape double quotes in the HTML, you could use a different delimiting method like heredoc or nowdoc. And perhaps it would be simpler to move the styles into CSS.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.