2

After upgrading my PHP version from 7.2 to 8.0.3, my custom error handler broke.

Here's the error: [date/time] Trying to access array offset on value of type null | err_error_log.php | line:36 (warn)

The error occurs here: switch ($lasterror['type']){

Here's the full code:

<?php
    set_error_handler("errorHandler");
    register_shutdown_function("shutdownHandler");
    function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context=""){
        $error = $error_message." | ".$error_file." | line:".$error_line;
        switch ($error_level) {
            case E_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_PARSE:
                mylog($error, "(fatal)");
                break;
            case E_USER_ERROR:
            case E_RECOVERABLE_ERROR:
                mylog($error, "(error)");
                break;
            case E_WARNING:
            case E_CORE_WARNING:
            case E_COMPILE_WARNING:
            case E_USER_WARNING:
                mylog($error, "(warn)");
                break;
            case E_NOTICE:
            case E_USER_NOTICE:
                mylog($error, "(info)");
                break;
            case E_STRICT:
                mylog($error, "(debug)");
                break;
            default:
                mylog($error, "(warn default)");
        }
    }
    function shutdownHandler(){ //will be called when php script ends.
        $lasterror = error_get_last();
        switch ($lasterror['type']){
            case E_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:
            case E_RECOVERABLE_ERROR:
            case E_WARNING:
            case E_CORE_WARNING:
            case E_COMPILE_WARNING:
            case E_USER_WARNING:
            case E_NOTICE:
            case E_USER_NOTICE:
            case E_STRICT:
            case E_PARSE:
                $error = $lasterror['message']." | ".$lasterror['file']." | line:".$lasterror['line'];
                mylog($error, "fatal");
        }
    }
    function mylog($error, $errlvl){
        error_log($error." ".$errlvl."\n"."https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI] ",0);
        $filename="tmp/variables.txt";
        if (file_exists($filename)){
            $bestand=fopen($filename, "r");
            $variables=fread($filename, filesize($filename));
            error_log("Variabels: ".$variables,0);
        }
    }
?>

Probably it's something stupid, but I can't seem to fix it... It has something to do with $lasterror['type'] being empty or something, if I understand it correctly from this topic: How to fix Trying to access array offset on value of type null error

Any help is appreciated! Thx in advance!

Fixed it like this:

function shutdownHandler(){ //will be called when php script ends.
        $lasterror = error_get_last();

        if ($lasterror!=null){
            switch ($lasterror['type']){ ...

Thx @El_Vanja

  • 4
    You're only seeing this because as of PHP 7.4, a warning is emitted in this case. Prior to that, you still had the same problem, it simply wasn't reported. As per [documentation](https://www.php.net/manual/en/function.error-get-last), `error_get_last` can return either an array or `null` and you never handled the second case. Add some code that covers the case when there is no error. – El_Vanja Apr 20 '21 at 14:50
  • 2
    Aside from that, what is the purpose of a `switch` whose cases all have identical handling? – El_Vanja Apr 20 '21 at 14:51
  • 2
    And another note: this shouldn't actually break your custom handler. It's not an error, it's just a warning and the result the code produces should be the same as before. – El_Vanja Apr 20 '21 at 14:53
  • 1
    @El_Vanja:I think I got the code from here and adjusted it a bit: https://stackoverflow.com/questions/1900208/php-custom-error-handler-handling-parse-fatal-errors/20809814 –  Apr 21 '21 at 06:43
  • 1
    I fixed it like this: $lasterror = error_get_last() ?? null; if ($lasterror!=null){ switch ($lasterror['type']){ ... –  Apr 21 '21 at 10:07
  • 1
    I like to keep my php errorlog clean, so I needed to fix it :-) –  Apr 21 '21 at 11:58
  • 1
    Naturally. Getting rid of warnings and notices should be everyone's goal. One thing though, you don't really need the `?? null` part, because `error_get_last()` already returns `null`. What `?? null` does here is "if this is `null`, assign `null`". – El_Vanja Apr 21 '21 at 12:02
  • 1
    Oh ic, I'll delete that then, thx again! :-) –  Apr 21 '21 at 12:05

0 Answers0