@$array['possible_missing_key'] 是反模式吗?

发布于 2024-10-07 13:16:17 字数 288 浏览 3 评论 0原文

从 PHP 数组中提取可能缺失的值时可以使用 @ 吗?示例:

$value = @$array['possibly_missing_key'];

预期行为:

if (isset($array['possibly_missing_key'])) {
    $value = $array['possibly_missing_key'];
} else {
    $value = null;
}

我想在传播使用模式之前知道。

Is it OK to use @ when extracting a possibly missing value from a PHP array? Example:

$value = @$array['possibly_missing_key'];

The intended behavior:

if (isset($array['possibly_missing_key'])) {
    $value = $array['possibly_missing_key'];
} else {
    $value = null;
}

I want to know, before spreading the usage pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瘫痪情歌 2024-10-14 13:16:17

@ 运算符会抑制错误消息,并且使用它可能会为您的代码设置其他错误和意外行为,而这些错误和意外行为最终很难追踪。因此,它肯定是一种反模式。

因此,我非常喜欢第二位。它更清楚地表明

  • 它可能不存在于数组中,以及
  • 如果不存在则默认值是什么

为了使其更简洁,您可以使用三元条件运算符 ?:,如所示马克·贝克的回答。代码稍少,符号较多,但含义很好理解。

The @ operator suppresses error messages, and using it potentially sets up your code for other errors and unexpected behavior that end up hard to track down. Thus it's most certainly an antipattern.

Thus, I would very much prefer the second bit. It makes it much clearer

  • that it may not be present in the array, and
  • what the default value is if it's not present

To make it more concise you can use the ternary conditional operator ?:, as seen in Mark Baker's answer. Slightly less code and more symbols but the meaning is well-recognized.

耳根太软 2024-10-14 13:16:17

实际上,isset 变体是反模式。如果您只是使用 isset($var)?$var:NULL 来抑制“错误”,那么与使用正确的语法来抑制错误相比,您并没有取得任何成果。它具有相同的结果,但可读性较差。

人们之所以争论这一点,是因为人们认为“清洁”,并且使用 isset 是一种微观优化。避免 @ 并使用 isset 作为语法盐替换只是货物崇拜编程。

Actually the isset variation is the anti-pattern. If you just use isset($var)?$var:NULL with the intention to suppress the "error", then you've achieved nothing over using the proper syntax for suppressing errors. It has the same outcome, yet is less readable.

People are arguing for that because of perceived "cleanliness" and because using isset is a micro optimization. Avoiding @ and using isset as syntactic salt replacement is just cargo cult programming.

日久见人心 2024-10-14 13:16:17

或者

$value = (isset($array['possibly_missing_key'])) ? $array['possibly_missing_key']: null;

Or

$value = (isset($array['possibly_missing_key'])) ? $array['possibly_missing_key']: null;
水晶透心 2024-10-14 13:16:17

忽略警告绝对是一种反模式;所以,是的,这是一种反模式(我可以保证,如果你学会抑制警告,其中一个警告会回来咬你的后背,如果不是更糟的话)。

此外,虽然第二个版本更详细,但它为未初始化的变量提供了已知状态(或者可以用于处理问题,如果变量应该被填充)。

Ignoring warnings is definitely an antipattern; so yes, it's an anti-pattern (and I can guarantee that if you learn to suppress warnings, one of them will come back and bite you in the posterior, if not worse).

Also, while the second version is more verbose, it gives the uninitialized variable a known state (or can be used to handle the problem, if the variable is supposed to be filled).

恋竹姑娘 2024-10-14 13:16:17

第三种选择:

$value = (isset($array['key']) ? $array['key'] : null);

我知道这并不能直接回答问题;我会把它作为评论,除非它确实需要格式化。

这里的想法是,如果您试图通过使用单行代码而不是 if-else 块来缩短代码,那么您仍然可以使用三元运算符将其变成简洁的单行代码,从而为您提供最好的结果两个世界的。

The third option:

$value = (isset($array['key']) ? $array['key'] : null);

I know this doesn't directly answer the question; I would have put it as a comment, except it really needed to be formatted.

The idea here is that if you're trying to make your code shorter by using a one-liner instead of an if-else block, then you can still get it into a succinct one-liner using a ternary operator, giving you the best of both worlds.

旧人 2024-10-14 13:16:17

第二个代码块(或者 Mark Ba​​ker 的替代方案,其工作原理完全相同)更好。我对 PHP 不太确定,但在许多其他编程语言中,简单地忽略变量几乎肯定会引发错误。至少在第二个块中,您将变量初始化为某个值或内存位置。

如果您期望函数在最终产品中引发预期错误(但是,大多数时候情况并非如此),则应该更常用错误抑制。

祝你好运!
丹尼斯·M.

The second block of code (or Mark Baker's alternative which will work exactly the same) is better. I'm not entirely sure about PHP, but in many other programming languages, to simply ignore a variable would almost definitely throw an error. At least with the second block you are initializing the variable to some value or memory location.

Error suppression should be more commonly used if you expect a function to throw an expected error in the end-product (however, much of the time this will not be the case).

Good luck!
Dennis M.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文