如何正确设置PHP set_error_处理程序

如何正确设置PHP set_error_处理程序,php,error-handling,custom-error-handling,Php,Error Handling,Custom Error Handling,你能帮我正确设置php设置错误处理程序吗?我需要处理将php警告作为异常抛出的代码。但我不知道如何正确地做。现在我有了这个代码,我认为它是不正确的 set_error_handler( function( $errno, $errstr, $errfile, $errline, array $errcontext ) { throw new \Exception( $errstr, $errno ); }); $mailer->send( $mail ); restore_erro

你能帮我正确设置php设置错误处理程序吗?我需要处理将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警告包含到异常中。Thx.

所以在我的例子中,必须有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; 
}); 

所以在我的例子中,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; 
});