一种方法而不是@
显而易见的方法是使用set_error_handler
https://www.w3schools.com/php/func_error_set_error_handler.asp
这使您可以将错误通过管道传递到ErrorException
类中,然后您会遇到异常而不是错误。这允许您使用try/catch
块来处理错误。
set_error_handler(function($severity, $message, $file = 'Unknown', $line = 'Unknown'){
//typically I set a constant for PHP_ERRORS for the exception code.
if (error_reporting() != -1 && !(error_reporting() & $severity)) {
//we'll let this error go to the next error handler
return; //return null
}else{
//convert the error into an exception
throw new ErrorExcption($message, 0, $severity, $file, $line );
//we don't have to return anything because the exception throwing kicks us out of the error handler.
}
});
try{
$answer=fgets($socket, $negotiatedMaxLength);
}catch(ErrorException $e ){
}
请注意,&
单个&符号是为了检查您所做的严重级别 -vs- 错误报告级别bitwise And
$file
和也是$line
可选的,所以我们为它们设置了一个默认值。