1

你能帮我正确设置 php set_error_handler吗?我需要处理将 php警告作为异常抛出的代码。但我不知道如何正确地做到这一点。现在我有了这段代码,我认为这是不正确的。

set_error_handler( function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
    throw new \Exception( $errstr, $errno );
});
$mailer->send( $mail );
restore_error_handler(); 

正如我在文档中看到的那样,它需要更复杂的解决方案。但我对所有这些常数有点困惑。有没有办法以某种优雅的方式设置它,不会单独设置所有常量。我的意思是:

function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
    if( $errno >= E_USER_WARNING ) throw new \Exception( $errstr, $errno );
});

那么如何以某种优雅的方式将php警告包含到异常中。谢谢。

4

1 回答 1

0

所以在我的情况下,errno 必须有一个条件,它必须返回 true。

set_error_handler( function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
    // Cause Php does not catch stream_socket_client warnings message.
    if ( $errno == E_WARNING ) throw new \Exception( $errstr, $errno ); 
    // Is necessary to handle other errors by default handler.
    return true; 
}); 
于 2016-11-13T21:09:40.043 回答