Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscores in magento variables

Tags:

magento

I can't understand and find information why there are sometimes underscores in variable names, for example $_links instead of $links.

What does it mean?

like image 405
deem Avatar asked Nov 19 '13 20:11

deem


People also ask

Can underscores be used in variables?

The underscore in variable names is completely optional. Many programmers use it to differentiate private variables - so instance variables will typically have an underscore prepended to the name. This prevents confusion with local variables.

What is underscore in variable?

A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.

Can variables have underscores Java?

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign " $ ", or the underscore character " _ ".


2 Answers

Underscores are used in two different ways in the Magento codebase.

In classes an underscore at the beginning of a variable or function name indicates that the variable is private or protected.

Within templates, most variables that are used locally are prefixed with an underscore. This indicates that the variable is "private" to the template.

like image 66
Jim OHalloran Avatar answered Oct 09 '22 20:10

Jim OHalloran


Mostly this is done to protect your template vars from having collisions with view variables.

In Magento CE 1.9.x.x

Mage_Core_Block_Template::fetchView - ln 215 extract ($this->_viewVars, EXTR_SKIP);

See reference for extract function: function_extract.

Hope, it helped.

like image 2
Wera Avatar answered Oct 09 '22 22:10

Wera