在trigger_error之前,“ @”(沉默运算符,符号处)的作用是什么? [英] What is the purpose of '@' (silence operator, at sign) before trigger_error?

查看:78
本文介绍了在trigger_error之前,“ @”(沉默运算符,符号处)的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多Symfony捆绑软件(以及其他代码)中都看到以下行:

I have seen in many Symfony bundles (and also in other codes) this line :

@trigger_error('The class is deprecated', E_USER_DEPRECATED);

根据文档, @ ( at)运算符用于使错误静音( http://php.net/

According to the documentation, the @ (at) operator is used to silence the errors (http://php.net/manual/en/language.operators.errorcontrol.php).

那么,抛出一个沉默错误的目的是什么?

So, what is the purpose of throwing a silenced error?

推荐答案

Symfony编码约定


没有 @-沉默运算符,用户需要退出弃用通知。沉默可以交换这种行为,并允许用户在准备好应对时选择加入(通过添加自定义错误处理程序,例如Web调试工具栏或PHPUnit桥所使用的错误处理程序)。

Without the @-silencing operator, users would need to opt-out from deprecation notices. Silencing swaps this behavior and allows users to opt-in when they are ready to cope with them (by adding a custom error handler like the one used by the Web Debug Toolbar or by the PHPUnit bridge).

评论作者关于相关PR的信息:

Comment from author about the relevant PR:


...还有其他考虑因素使我认为这确实是最好的:性能。我们可以将触发选择弃用通知的实现包装在更多语义接口的后面。但这会增加一些需要尽快处理的开销。当可以调用数千次时,这不是一个微优化。
因此,这种原始的裸金属php实现可能看起来很笨拙,但是恰好满足了我们的需求。

...There is an other consideration that makes me think this is really the best: performance. We could wrap the implementation of triggering "opt-in deprecation notices" behind some more semantic interface. But that would add overhead to something that needs to be as fast as possible. This is not a micro-optim when this can be called thousands of times. Thus this raw-bare-metal-php implementation that may look hacky, but is just the right implementation for our needs.

这篇关于在trigger_error之前,“ @”(沉默运算符,符号处)的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆