简体   繁体   English

PHP使用@转义错误报告

[英]PHP escaping error reporting with @

I am currently refactoring some code for work and I have come across some function calls prefixed by the "@" symbol. 我正在重构一些工作代码,我遇到了一些以“@”符号为前缀的函数调用。 As I understand it, this is intended to escape PHP error reporting if the call fails. 据我了解,如果调用失败,这是为了逃避PHP错误报告。

Is this type of thing good practice? 这种类型的东西是好的做法吗? I understand the rationale in a development environment but when the site is pushed to production shouldn't all errors be handled properly rather than just escaped? 我理解开发环境中的基本原理,但是当网站被推向生产时,不应该正确处理所有错误而不是仅仅进行转义?

The use of this symbol would therefore mean that the developer has to sort through the code at a later stage to remove all error reporting escapes. 因此,使用此符号意味着开发人员必须在稍后阶段对代码进行排序,以删除所有错误报告转义。

I am unsure whether to remove these symbols and just find a better way to handle potential errors or not. 我不确定是否删除这些符号,只是找到一种更好的方法来处理潜在的错误。

For clarity, the function this was used on was the native PHP fsockopen() function. 为清楚起见,使用它的函数是本机PHP fsockopen()函数。

That's probably among the worst practices you can come across in php code. 这可能是你在PHP代码中遇到的最糟糕的做法。 It basically tells the interpreter to suppress errors and just try to do whatever the code asks it to do regardless of the outcome. 它基本上告诉解释器抑制错误,并尝试做任何代码要求它做的事情,无论结果如何。

A great way to drag yourself and fellow teammates into all-nighter phantom bug hunts once the app has grown substantially. 一旦应用程序大幅增长,一个很好的方法可以将自己和其他队友拖入全能的幻影中。

Try-catch with custom exception handling is the way to go. 使用自定义异常处理的try-catch是可行的方法。

I think it is sometimes understandable to use @ for calling functions like fsockopen(), because when they fail they will raise a warning as well as returning false. 我认为使用@来调用函数如fsockopen()有时是可以理解的,因为当它们失败时它们会引发警告并返回false。

There may be cases where you expect these calls to fail regularly and therefore do not want a warning to be raised. 在某些情况下,您可能希望这些调用定期失败,因此不希望发出警告。 Obviously you shouldn't be displaying warnings in production and should be logging them instead, but you might still want to use the @ operator to stop your logs getting full. 显然,您不应该在生产中显示警告,而应该记录它们,但您可能仍希望使用@运算符来阻止日志变满。 You could stop warnings getting reported at all by changing the error_reporting setting but that is not ideal. 您可以通过更改error_reporting设置来停止报告,但这并不理想。

That's called the error control operator , and is generally a very scary thing to consider using. 这被称为错误控制操作符 ,通常是一个非常可怕的事情要考虑使用。 A warning from the manual (the emboldening is mine): 手册中的警告(我的是大胆的):

Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution . 目前,“@”错误控制运算符前缀甚至会禁用 将终止脚本执行的 严重错误的错误报告 Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why . 除此之外,这意味着如果你使用“@”来抑制来自某个函数的错误,并且它不可用或者输入错误,那么脚本就会在那里死亡而没有任何 关于原因的指示

Using the "@" operator is very useful when you know that the function call can fail, like, for example, the fsockopen call. 当您知道函数调用可能失败时,使用“@”运算符非常有用,例如, fsockopen调用。 Best practice is to use this only when the function you are calling often fails and is a valid case in your application. 最佳做法是仅在您调用的函数失败并且在您的应用程序中是有效的情况时才使用它。 Also, you should definitely check the return value of the function after calling it: 此外,您应该在调用后检查函数的返回值:

$fp = @fsockopen($hostname, $port);
if ($fp === false) {
    // handle connection failure
}
else {
    // handle connection success
}

You should avoid two things: 你应该避免两件事:

  1. Not checking the return value; 不检查返回值;
  2. Using the "@" operator where you don't expect an error -- for example when opening a local file or sending headers. 使用“@”运算符,您不希望出现错误 - 例如打开本地文件或发送标题时。 When opening a local file fails, that is an error and it should be handled properly. 打开本地文件失败时,这一个错误,应该正确处理。

Note: you might also want to look at set_error_handler() 注意:您可能还想查看set_error_handler()

if you use your custom error-handlers, the @ operator will not help you, you will always get error-events from situations where your are handling the "Warning" in your code ... like at fsockopen etc. 如果您使用自定义错误处理程序,@运算符将无法帮助您,您将始终从处理代码中的“警告”的情况中获得错误事件...例如在fsockopen等。

so you can simple suppress effectively the warning this way: 所以你可以这样简单地有效地抑制警告:

function renameWithOutExpectedAndSelfHandledErrors( ... ) {

  set_error_handler(function(){}); // deactivate all errors
  $result = rename('not existing','blafussel');
  restore_error_handler(); // restore old error-situation

  return $result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM