dou47732
2017-12-09 03:28
浏览 51
已采纳

如何在不禁用错误报告的情况下抑制fputs,fsockopen等警告?

I use fsockopen, fgets and fputs to implement communication protocol with other machine. NetBeans give warning to all '@' before fsockopen, fputs, fgets etc. The solution works but without '@' after disconnection of remote device there are warnings (not errors).

I don't want to use error_reporting because it is not more kosher solution. Additionally more code, longer execution time...

Is there any better solution for this?

BTW. the warnings occurs if the destination machine will drop connection. It is possible if device is overload.

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

BTW. The solution should work without ini_set - blocked on server and without error_reporting().

图片转代码服务由CSDN问答提供 功能建议

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

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

有没有更好的解决方案?

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

  $ answer = @ fgets($ socket,$ negotiatedMaxLength); 
   
 
 <  p>顺便说一句。 该解决方案应该在没有 ini_set 的情况下工作 - 在服务器上被阻止而没有 error_reporting() 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dousou1967 2017-12-09 03:36
    已采纳

    One way instead of @ which is obvious way to do it, is to use set_error_handler

    https://www.w3schools.com/php/func_error_set_error_handler.asp

    This lets you pipe the errors into the ErrorException class and then you have exceptions instead of errors. That's allows you to use try/catch blocks for the errors.

    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 ){
    
     }
    

    Note the & single ampersand is intentional for checking severity level -vs- error reporting level you do bitwise And

    Also the $file and $line are optional, so we set a default for them.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题