在PHP中哪个更好?用’@’抑制警告或用isset()运行额外的检查?

前端之家收集整理的这篇文章主要介绍了在PHP中哪个更好?用’@’抑制警告或用isset()运行额外的检查?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
例如,如果我实现一些简单的对象缓存,哪个方法更快?
1. return isset($cache[$cls]) ? $cache[$cls] : $cache[$cls] = new $cls;

2. return @$cache[$cls] ?: $cache[$cls] = new $cls;

我读了某个地方@需要大量时间执行(我不知道为什么),特别是当警告/通知实际上被发出和被禁止的时候. isset()另一方面意味着额外的哈希查找.那么哪个更好,为什么?

我确实希望在开发和生产服务器上保持全球的E_NOTICE.

isset()方法更好.它是明确声明索引可能未定义的代码.抑制错误是粗糙的编码.

根据这篇文章10 Performance Tips to Speed Up PHP,警告需要额外的执行时间,并声称@运算符是“昂贵的”.

Cleaning up warnings and errors beforehand can also keep you from
using @ error suppression,which is expensive.

此外,@不会抑制自定义错误处理程序的错误

http://www.php.net/manual/en/language.operators.errorcontrol.php

If you have set a custom error handler function with
set_error_handler() then it will still get called,but this custom
error handler can (and should) call error_reporting() which will
return 0 when the call that triggered the error was preceded by an @.

If the track_errors feature is enabled,any error message generated by the expression will be saved in the variable $PHP_errormsg. This variable will be overwritten on each error,so check early if you want to use it.

原文链接:https://www.f2er.com/php/131151.html

猜你在找的PHP相关文章