0

我使用 fsockopen、fgets 和 fputs 来实现与其他机器的通信协议。NetBeans 在fsockopen, fputs, fgets等之前向所有“@”发出警告。该解决方案有效,但在远程设备断开连接后没有“@”有警告(不是错误)。

我不想使用 error_reporting 因为它不是更犹太的解决方案。此外更多的代码,更长的执行时间......

有没有更好的解决方案?

顺便提一句。如果目标计算机将断开连接,则会出现警告。如果设备过载,这是可能的。

$answer=@fgets($socket, $negotiatedMaxLength);

顺便提一句。该解决方案应该ini_set在服务器上不受阻止且没有error_reporting().

4

1 回答 1

1

一种方法而不是@显而易见的方法是使用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可选的,所以我们为它们设置了一个默认值。

于 2017-12-09T03:36:23.237 回答