php:是否有充分的理由引用所有数组键/索引? [英] php: Are there great reasons to quote all array keys/indexes?

查看:42
本文介绍了php:是否有充分的理由引用所有数组键/索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行其他人的代码,他们不断避免转义他们的数组键.

I'm running through someone else's code, and they constantly avoid escaping their array keys.

例如:

$row_rsCatalogsItems[名称]

$row_rsCatalogsItems[Name]

代替

$row_rsCatalogsItems['Name']

$row_rsCatalogsItems['Name']

所以我不断地对我接触的所有东西进行微小的改变,以应对那点懒惰.但现在我想知道这样做是否有很多好处.我知道它会在默认为字符串之前检查常量(我在处理常量时讨厌 php 中的这种行为,因为即使没有定义它们也会验证为真),但我不确定这是否值得麻烦更改常量作为数组键的所有许多实例.

so I'm constantly making tiny changes to everything I touch just to deal with that bit of laziness. But now I'm wondering whether there's even much benefit to doing so. I get that it'll check for constants before defaulting to a string (I detest that behavior in php when dealing with constants, since they validate to true even if not defined), but I'm not sure even that is worth the bother of changing all the many, many instances of constants-as-array-keys.

建议?

推荐答案

原因不止一个.

error_reporting(E_ALL);

在脚本的开头,您将立即看到所有原因.

in the beginning of your script and you will see all the reasons at once.

抛开乐趣不谈,在实践中您可能会认为,这是我可以逃脱的事情.你可能是对的.但是作为开发人员,您必须在什么是可接受的黑客攻击以及口头辱骂的原因方面划清界限.对我来说,这种特殊的行为超出了这条线.

Fun aside, in practice you might think that well, this is something that I might get away with. And you could be right. But as a developer you have to draw the line somewhere about what is an acceptable hack and what is reason for verbal abuse. For me, this particular behavior is beyond that line.

这很糟糕的直接原因是它使 error_reporting(E_ALL) 无法使用.良好的开发实践要求报告所有错误,这些通知让您准备迎接更复杂的代码和更难的调试会话.

The very immediate reason why this is bad is that it makes error_reporting(E_ALL) unusable. And good development practice demands that all errors are reported, these notices are setting you up for buggier code and harder debugging sessions.

更新:我没有解决现有情况的实际解决方案的问题,所以我会在你看来:

Update: I didn't address the issue of a practical solution to the existing situation, so here's what I would do in your shoes:

  1. 找到责任人并确保他们永远不会再以任何必要的方式这样做.
  2. 运行有问题的脚本,并从日志文件中获取有关未定义常量的所有通知.
  3. 使用编辑器中的正则表达式搜索和替换,尝试将 \[(a-zA-Z_-)\] 替换为 ['$1'](或其他相似的).如果替换的数量等于日志文件中的通知数量,那么您就是黄金.否则,尝试分而治之,直到您看到正则表达式失败的地方.
  4. 根据需要对所有其他脚本重复.
  1. Find the person responsible and make sure that they never, ever do this again by any means necessary.
  2. Run a problematic script and get all the notices about undefined constants from the log file.
  3. Using regex search and replace from your editor, try to replace \[(a-zA-Z_-)\] to ['$1'] (or something similar). If the number of replacements is equal to the number of notices in the log file, you are golden. Otherwise, try divide and conquer until you see where the regex is failing.
  4. Repeat as required for all the other scripts.

这篇关于php:是否有充分的理由引用所有数组键/索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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