开发者

PHP set warnings as fatal

Is there a way to specify in php.ini or apache level that the script execution should halt on any warnings? I tried searching through google and stackoverflow but couldn't find any relevant information yet. I do know about the set_error_handler() functi开发者_StackOverflow中文版on but I am looking to do this at the php.ini level.


EDIT Sep 2021: OP does not want this solved by using a custom error function like I propose, but this question gets quite some traffic by people who are perfectly happy using a custom error function.

// Custom error function (even triggers for warnings)
set_error_handler(function($severity, $message, $file, $line) {
    if (error_reporting() & $severity) {
        throw new ErrorException($message, 0, $severity, $file, $line);
   }
});

In the project I am using this code in, I made this little helper function to switch at different parts of the project. Possible values are 0, 1 and 2

function updateErrorHandling($state) {
    //custom error handler or standard
    if ($state > 1) {
        // Custom error function (even triggers for warnings)
        set_error_handler(function($severity, $message, $file, $line) {
            if (error_reporting() & $severity) {
                throw new ErrorException($message, 0, $severity, $file, $line);
            }
        });
    } else {
        // Standard Exception Handling on / off
        ini_set('display_errors', $state);
        ini_set('display_startup_errors', $state);
        error_reporting($state == 1 ? E_ALL : 0);
    }
}


AFAIK, there is no way to do something like that at the configuration level. However, I think it could be done using PHP's set_error_handler function.

http://php.net/manual/en/function.set-error-handler.php

Within the function you set, can detect a warning via the ERROR_TYPE parameter, and halt the script using exit or die.


So like always it's a little difficult to prove a negative like you're asking. About 14 seconds of googling yields the complete list of php.ini directives:

http://www.php.net/manual/en/ini.list.php

There are only 4 instances of the word 'warning' on this page, so it looks like that exotic idea is not an option.


I would recommend xdebug.halt_level

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜