Magento Class Overrides

As we know in magento custom modules, we can override core magento classes (model, helper, blocks) through config.xml file. In this blog post we will see how magento implements this pattern.

Model, Blocks, Helper Overriding

Lets see in code how magento implements this. We know the base functions which magento uses to create model, block, helper instances are

Mage::getModel()
Mage::getBlock()
Mage::helper()

These function call “getBlockClassName()” , “getHelperClassName()” , “getModelClassName()” all located in “Mage_Core_Model_Config” class.
So when we load a model using function Mage::getModel('catalog/product'); we are actually calling the “getModelClassName()” function in the Mage_Core_Model_Config class.

All the above functions in turn call “getGroupedClassName()” function with their respective groups “model”, “helpers”, “blocks”.

Function “getGroupedClassName()” takes $groupType and $classId as parameters.

$groupType is ‘model’ or ‘block’ or ‘helper’
$classId is the short name like ‘core/website’ , ‘catalog/product’ etc

$classArr = explode('/', trim($classId));
$group = $classArr[0];
$class = !empty($classArr[1]) ? $classArr[1] : null;

Next this xpath “global->{$groupType}s->{$group}->rewrite->{$class}” is created and checked if this node exists in configuration.
So for a model ‘catalog/product’ it will form the xpath “global->models->catalog->rewrite->product”
This is the same xpath we defined in our module xml file to override blocks, helpers, and model.
If the rewrite node is found that class object is created instead of the default.