Short version: if you do not know what it is for, you do not need to use it.
The following is a long version.
This has nothing to do with underscoe.js. Before it was a javascript framework, underscore was a simple ASCII _ character.
In Magento, most objects are inherited from the database.
Varien_Object
class. The object provides the special functionality of the Magento object. For example, in a Magento object you do not need to define setters and getters. You can just do something like this
$object = new SomeObject; //which inherits form Varien_Object $object->setSomeValue('Our Value'); echo $object->getSomeValue('Our Value'); echo $object->getData('our_value'); $data = $object->getData(); echo $data['our_value'];
In the above example, there is no specific method named setSomeValue . Magento magically knows that we just want to set the data property. This is implemented in the PHP __call magic method __call
#File: lib/Varien/Object.php public function __call($method, $args) { ... }
When you call setSomeValue , Magento sets the key in the object's data array called some_value . That is, it must convert a camel with a SomeValue body to a body without some_value camel. You can see it in the magic __call implementation here
#File: lib/Varien/Object.php public function __call($method, $args) { case 'set' : //Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method); $key = $this->_underscore(substr($method,3)); $result = $this->setData($key, isset($args[0]) ? $args[0] : null); //Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method); return $result; }
The _underscore method takes a _underscore string and converts it to some_value with the following string
#File: lib/Varien/Object.php $result = strtolower(preg_replace('/(.)([AZ])/', "$1_$2", $name));
At some point, through profiling or intuition, the Magento developer realized that when calling strToLower and preg_replace each time the data was received or installed on the object, which means the performance of the bottleneck. To fix this, they introduced _underscoeCache . This is an array and a static property in the Varien_Object class
#File: lib/Varien/Object.php protected static $_underscoreCache = array();
If you look at the whole _underscore method, you will see how it used
protected function _underscore($name) { if (isset(self::$_underscoreCache[$name])) { return self::$_underscoreCache[$name]; }
That is, Magento will still perform strToLower and preg_replace , but it will only perform it once for each unique row. After that, the key / value ( SomeValue and some_value ) is placed in _underscoreCache
self::$_underscoreCache[$name] = $result;
Thus, the next time the method is called, a cache value is returned instead.
if (isset(self::$_underscoreCache[$name])) { return self::$_underscoreCache[$name]; }
This avoids calls to strToLower and preg_replace for public variables.