加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:12:32 所属栏目:PHP教程 来源:网络整理
导读:例如,如果我实现一些简单的对象缓存,哪个方法更快? 1. return isset($cache[$cls]) ? $cache[$cls] : $cache[$cls] = new $cls;2. return @$cache[$cls] ?: $cache[$cls] = new $cls; 我读了某个地方@需要大量时间执行(我不知道为什么),特别是当警告/通知实
例如,如果我实现一些简单的对象缓存,哪个方法更快?
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读