在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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...