What is _underscoreCache in Magento

I missed some code using xDebug and constantly notice _underscoreCache as an array of kv pairs:

object collection is_default default_group_id front store_id action session_hosts controller_action request secure visitor_data website quote items_collection parent_item_id quote_id item_id product_id code attributes website_id attribute_set_id additional_attribute_table attribute_codes is_global skip_confirmation_if_email confirmation visibility is_salable stock_item udropship_vendor custom_vars_combined password_enc vendor_name use_local_stock is_recurring customer_group_id date product_collection product_name product_type_id product_status_changed is_changed_websites product_changed_websites license_key license_key license_status license_expire server_restriction products module_name use_config_manage_stock is_in_stock stock_status product_type sku name weight tax_class_id cost base_cost is_qty_decimal quote_item product message item is_super_mode qty_to_add is_child_item has_error item_is_qty_decimal has_qty_option_update item_qty orig_qty use_config_min_sale_qty use_config_max_sale_qty suppress_check_qty_increments use_config_enable_qty_increments use_config_min_qty item_use_old_qty item_backorders ignore_old_qty use_old_qty skip_check_required_option ud_skip_quote_load_after_event order has_children vendor_sku address_type address postcode limit_zipcode country_id allowed_countries stockcheck_method true_stock udropship_stock_levels stock_result quote_currency_code currency_code customer_id prefix customer_prefix firstname customer_firstname middlename customer_middlename lastname customer_lastname suffix customer_suffix email customer_email dob customer_dob taxvat customer_taxvat gender customer_gender customer_tax_class_id remote_ip x_forwarded_for items_count checkout_method checkout_state collect_shipping_rates totals_collected_flag extra_tax_amount base_extra_tax_amount subtotal base_subtotal subtotal_with_discount base_subtotal_with_discount grand_total base_grand_total quote_address is_multi_shipping total_qty base_virtual_amount virtual_amount subtotal_incl_tax base_subtotal_incl_tax applied_rule_ids shipping_tax_amount base_shipping_tax_amount applied_taxes_reset free_shipping free_method_weight region_id customer_class_id store parent_id product_class_id shipping_amount base_shipping_amount shipping_incl_tax base_shipping_incl_tax shipping_taxable base_shipping_taxable is_shipping_incl_tax base_cod_fee cod_fee cod_tax_amount base_cod_tax_amount method prev_quote_customer_group_id discount_amount base_discount_amount base_calculation_price calculation_price base_original_price original_custom_price rate row_total base_row_total coupon_code no_discount coupon_type uses_per_customer is_primary uses_per_coupon use_auto_generation usage_limit operator_option operator_by_input_type value_option operator_options type aggregator value actions aggregator_option aggregator_options rule conditions_serialized attribute_option attribute operator is_value_parsed value_parsed actions_serialized simple_free_shipping stop_rules_processing tax_percent custom_price base_price price_incl_tax base_price_incl_tax row_total_incl_tax base_row_total_incl_tax taxable_amount base_taxable_amount is_price_incl_tax rounding_deltas weee_tax_applied base_weee_tax_disposition weee_tax_disposition base_weee_tax_row_disposition weee_tax_row_disposition base_weee_tax_applied_amount base_weee_tax_applied_row_amount weee_tax_applied_amount weee_tax_applied_row_amount row_weight all_items dest_country_id dest_region_id dest_region_code dest_street city dest_city dest_postcode package_value package_value_with_discount package_weight package_qty package_physical_value base_currency package_currency limit_carrier orig path cipher mode handler init_vector active_flag error weight_type full_row_weight carrier_code zip ups_pickup ups_container ups_dest_type udropship_calculate_rates calculate_rates_by_group_flag requests website_ids system_methods 

What is it? I thought it would be a window in js underscore, similar to a mage registry? How can I use it? How is it populated?

+4
source share
2 answers

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 /** * Setter/Getter underscore transformation cache * * @var array */ 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]; } #Varien_Profiler::start('underscore'); $result = strtolower(preg_replace('/(.)([AZ])/', "$1_$2", $name)); #Varien_Profiler::stop('underscore'); self::$_underscoreCache[$name] = $result; return $result; } 

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.

+10
source

perfect clarification, but I can "take var from the frontend control system." although i see it in $ _underscoreCache

i use:

 Mage::getSingleton('core/session')->getCartWasUpdated(); 

and I want to take var, which is set to cartControlller.php

  $this->_getSession()->setCartWasUpdated(true); 
0
source

Source: https://habr.com/ru/post/1499038/


All Articles