您能帮我正确设置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();
正如我在documentation中看到的那样,它需要更复杂的解决方案。但是我对所有这些常数都不太困惑。
有没有一种以某种优雅的方式设置它的方法,它不会单独设置所有常量。
我的意思是:
function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
if( $errno >= E_USER_WARNING ) throw new \Exception( $errstr, $errno );
});
因此,如何以某种优雅的方式包含php警告Exception。谢谢。
最佳答案
因此,在我的情况下,必须满足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;
});
关于php - 如何正确设置PHP set_error_handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574576/