为什么 Magento 中的局部变量有一个下划线前缀?

发布于 2024-09-27 22:05:55 字数 311 浏览 5 评论 0原文

作为之前问题的后续 我想知道是否有人知道为什么 Magento 模板都用下划线声明变量。模板是来自 Mage_Core_Block_Template::fetchView() 的 .phtml 文件 include-ed,它们的局部变量在函数结束时被释放,并且永远不会成为全局变量。

那么下划线有什么意义呢?

As a follow up to an earlier question I wonder if anyone knows why Magento templates all declare their variables with an underscore. Templates are .phtml files include-ed from Mage_Core_Block_Template::fetchView(), their local variables are disposed of by the end of the function and never become global.

So what's the point of an underscore?

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

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

发布评论

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

评论(2

心是晴朗的。 2024-10-04 22:05:55

我认为这是为了尊重ZF的命名约定:

http://framework.zend.com/manual/en/coding-standard.naming-conventions.html#coding-standard.naming-conventions.variables

对于使用“private”或“protected”修饰符声明的实例变量,变量名称的第一个字符必须是单下划线。这是变量名中唯一可接受的下划线应用。声明为“public”的成员变量绝不能以下划线开头。

I think it's to respect ZF's naming convention:

http://framework.zend.com/manual/en/coding-standard.naming-conventions.html#coding-standard.naming-conventions.variables

For instance variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared "public" should never start with an underscore.

始终不够 2024-10-04 22:05:55

这里没有确定的答案,但我相信这只是 magento 团队内部的编码约定。我相信您通常都知道下划线前缀的历史。这样想并不过分

视图脚本中使用的与对象松散耦合的局部变量类似于私有或受保护的变量

换句话说,下划线充当警告:

嘿,这个变量的内容不仅仅属于这个视图,它还属于其他东西

部分问题是 Zend 或其他任何人都没有“官方”建议,在 phtml 文件中应该使用哪种命名约定。

No certain answer here, but I believe it's just an internal magento team coding convention. I'm sure you're aware of the underscore prefix's history in programming in general. It's not too much of a stretch to think that

local variables used in a view script that are loosely coupled with an object are similar to private or protected variables

In other words, the underscore serves as a warning that

hey, the contents of this variable doesn't just belong to this view, it's owned by something else

Part of the problem is there's no "official" recommendations from Zend, or anyone else, what sort of naming conventions should be used in phtml files.

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