php - 知道为什么我的 'register_shutdown_function'无法捕获 'require'错误吗?

标签 php error-handling

据我了解,register_shutdown_function是一种捕获PHP顶级错误的“有效”方式,如here所述。
现在-我有一个相当完整的错误处理实现,希望能够强制捕获初始化后PHP生成的每个错误。我这样做的代码如下所示:

// ErrorInitialization.php
if($GLOBALS["EnableReporting"])
    error_reporting($GLOBALS["ErrorLevel"]);
else
    error_reporting(0);

ini_set('display_errors',$GLOBALS["EnableReporting"]);
ini_set('display_startup_errors',$GLOBALS["EnableReporting"]);

set_error_handler($GLOBALS["ErrorCallback"]);
set_exception_handler($GLOBALS["ExceptionCallback"]);

register_shutdown_function($GLOBALS["FatalCallback"]);
为了彻底这是我定义的全局变量:
// Config.php - Required from global scope.
$ErrorCallback = "HandleRuntimeError";
$ExceptionCallback = "HandleException";
$FatalCallback = "HandleFatalError";

$EnableReporting = true;

$ErrorLevel = E_ALL;
而且;我的实际方法是要捕获上述错误:
// ErrorHandling.php
function HandleRuntimeError($ErrorLevel,$ErrorMessage,$ErrorFile,$ErrorLine,$ErrorContext)
{
}

function HandleException($Exception)
{
}

function HandleFatalError()
{
    $Error = error_get_last();
    
    // Pass fatal errors up to the standard error callback.
    $GLOBALS["ErrorCallback"]($Error['type'],$Error['message'],$Error['file'],$Error['line']);
}
现在-我现在所看到的方式,如果发生错误,我应该实际上没有任何输出(因为我的“处理程序”为空);但实际上我仍在获得“标准”报告-例如:
Fatal error: require(): Failed opening required '***/resource/php/startup/IncludeHandler.php' (include_path='.;C:\php\pear') in ***\index.php on line 18
哪一个;再次为了更彻底-看起来像这样:
// index.php
function Initialize()
{   
    require($GLOBALS["PHPRoot"] . "errors/ErrorHandling.php");
    require($GLOBALS["PHPRoot"] . "errors/ErrorInitialization.php");
    require($GLOBALS["PHPRoot"] . "startup/IncludeHandler.php"); // Line 18
}
故事的寓意:关于为什么没有调用“HandleFatalError”方法,有人能快速回答吗?
-我是通过打开display_errors来搞砸自己的吗?

最佳答案

如果INI配置选项'display_errors'或'display_startup_errors'设置为1(或true),则无论是否存在处理错误的方法,都会显示错误。

在这种情况下,我的解决方法是这样的:

编辑以添加全局

// config.php
// Name of your error callback method
$ErrorCallback = "HandleRuntimeError";
// Name of your exception callback method
$ExceptionCallback = "HandleException";
// Name of your 'shutdown' (fatal) callback method
$FatalCallback = "HandleFatalError";

// Whether or not to enable error reporting at all. Should be overridden by a 'debugging' cookie or some such probably.
$EnableReporting = true;
// Level Options:
// -1                :: Report all PHP errors (( Much like E_ALL ) regardless of future changes to the following flags )
// 0                 :: Disable All Reporting ( alternately handled using the above boolean )
// E_ALL             :: All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0.
// E_STRICT          :: Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
// E_DEPRECATED      :: Run-time notices. Enable this to receive warnings about code that will not work in future versions.
// E_ERROR           :: Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
// E_NOTICE          :: Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
// E_PARSE           :: Compile-time parse errors. Parse errors should only be generated by the parser.
// E_WARNING         :: Run-time warnings (non-fatal errors). Execution of the script is not halted.
// E_CORE_ERROR      :: Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
// E_CORE_WARNING    :: Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
// E_COMPILE_ERROR   :: Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
// E_COMPILE_WARNING :: Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
// E_USER_ERROR      :: User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
// E_USER_NOTICE     :: User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().
// E_USER_WARNING    :: User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
// E_USER_DEPRECATED :: User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error().
// Error reporting level
// Example: $ErrorLevel = E_PARSE | E_STRICT;
$ErrorLevel = E_ALL;

// ErrorInitialization.php  
if($GLOBALS["EnableReporting"])
{
    error_reporting($GLOBALS["ErrorLevel"]);

    if( isset($GLOBALS["ErrorCallback"]) && strlen($GLOBALS["ErrorCallback"]) > 0 )
    {
        $Callback = $GLOBALS['ErrorCallback'];

        set_error_handler($GLOBALS["ErrorCallback"]);
        ini_set('display_errors',false);
    }
    else
        ini_set('display_errors',$GLOBALS["EnableReporting"]);

    if( isset($GLOBALS["FatalCallback"]) && strlen($GLOBALS["FatalCallback"]) > 0 )
    {
        $Callback = $GLOBALS['FatalCallback'];

        register_shutdown_function($GLOBALS["FatalCallback"]);
        ini_set('display_startup_errors',false);
    }
    else
        ini_set('display_startup_errors',$GLOBALS["EnableReporting"]);

    if( isset($GLOBALS['ExceptionCallback']) && strlen($GLOBALS['ExceptionCallback']) > 0 )
        set_exception_handler($GLOBALS["ExceptionCallback"]);
}
else
{
    ini_set('display_errors',0);
    ini_set('display_startup_errors',0);
    error_reporting(0);
}   

关于php - 知道为什么我的 'register_shutdown_function'无法捕获 'require'错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115279/

相关文章:

php - 如何停止在后台运行的 PHP 脚本

php - 从MySQL服务器检索一定时间间隔内的数据

php - 有没有一种方法可以在未构造的类中使用CakeResponse对象?

go - 合并多个错误字符串

vbscript - 检测到 “Error: Object doesn' t 支持此属性或方法”

api - api POST Auth始终返回200,但出现401错误

php - 使用 Symfony2 更详细地记录错误

php - FOSUserBundle - 首次登录后强制更改密码

php - 使用 html 表单 (MYSQL/PHP) 将记录插入数据库

ios - 您应该如何处理生产环境中的错误?