Why do local variables in Magento have an underscore prefix?

前提是你 提交于 2019-12-10 17:53:46

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3949202/why-do-local-variables-in-magento-have-an-underscore-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!