Magento Certified Developer GuideCategory: 1. BasicsDescribe class group configuration and use in factory methods
Manish Prakash Staff asked 2 years ago

Magento provides many factory methods to create models, blocks, helpers like

Mage::getModel()
Mage::getSingleton()
Mage::getResourceModel()
Mage::getResourceSingleton()
Mage::getBlockSingleton()
Mage::helper()
Mage::getResourceHelper()

All these function calls
“getBlockClassName”, “getModelClassName()”, “getHelperClassName” instead depending on type.
These function instead call getGroupedClassName() in the ‘core/config’ class which is a factory method.

The definition of this method is

//lets see what happens when we do Mage::getModel('core/website')
public function getGroupedClassName($groupType, $classId, $groupRootNode=null){
       
        //groupType is model, block etc
        //in current case it is model
        if (empty($groupRootNode)) {
            $groupRootNode = 'global/'.$groupType.'s';
        }
        //grouprootnode becomes global/models

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

        if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
            return $this->_classNameCache[$groupRootNode][$group][$class];
        }

        $config = $this->_xml->global->{$groupType.'s'}->{$group};

        // First - check maybe the entity class was rewritten
        $className = null;
        //global->models->core->rewrite->website
        if (isset($config->rewrite->$class)) {
            $className = (string)$config->rewrite->$class;
        } else {
               //backward compatible code which is not important
        }
        if (empty($className)) {
            if (!empty($config)) {
                $className = $config->getClassName();
            }
            if (empty($className)) {
                $className = 'mage_'.$group.'_'.$groupType;
            }
            if (!empty($class)) {
                $className .= '_'.$class;
            }
            //mage_core_model_website
            $className = uc_words($className);
            //Mage_Core_Model_Website
        }

        $this->_classNameCache[$groupRootNode][$group][$class] = $className;
        return $className;
}

$groupType is ‘block’ , ‘model’ , ‘helper’ depending on the type of object we need.
$classId is name of the resource e.g ‘core/config’

The factory method getGroupClassName() dynamically generates full class names based on the above parameters.
This is function responsible for model/block/helper overrides as well.