dou47732 2017-12-08 19: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().

  • 写回答

1条回答 默认 最新

  • dousou1967 2017-12-08 19: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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部