PHP 5.4: disable warning “Creating default object from empty value”

前端 未结 4 1933
萌比男神i
萌比男神i 2020-12-10 05:16

I want to migrate code from PHP 5.2 to 5.4. This worked fine so far except that all the code I use makes extensive use of just using an object with a member without any init

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 05:57

    I know I could set the php error_reporting to not reporting warnings, but I want to be able to still get other warnings and notices.

    Don't disable the error reporting, leave it at an appropriate level, but disable the display_errors directive:

    ini_set('display_errors', 0);
    

    There's no reason to print notices to the screen in production.

    Then as Jon said, use a custom error handler if you aren't already, example:

    function my_error_handler($error_level, $error_message)
    {
        // write error message to log file
    }
    set_error_handler('my_error_handler');
    

    You can pass in the error reporting level to the second param of set_error_handler. This way you can take your time and do the right thing, which is fix the code to stop generating notices.

    If you're moving a lot of sites over to 5.4, you can probably have your server admin turn off display_errors in php.ini until you have enough time to fix everything. Better yet, stage everything and fix it before upgrading, but that may not be an option.

提交回复
热议问题