Class Initialization in Magento 1.x


Dependency Injection is a way of class initialization . Class Initialization was also available in Magento 1  so why dependency injection in Magento 2? To know the answer of this question lets see how object initialization was available in Magento 1 and what was the problem into it.

Object Initialization in Magento 1.x

Lets take a look of a model class in Magento 1

class Cutetehits_Employee_Model_Statusreport extends Mage_Core_Model_Abstract {

public function _construct() {

public function ModelMethod1()
//Some Operation

You wouldn’t see any code like

$statusreport = new Cutehits_Employee_Model_Statusreport();

Then how does this class gets initialized?

This class gets instantiated by ‘getModel’ method


Just for information ‘cutehits_employee’ is defined under config.xml that contain resourceModel class. For example if we are calling

Mage::getModel('cutehits_employee/statusreport') then the respective class would be Cutehits_Employee_Model_Statusreport

take a look of config.xml


The question still remain same, where does class initialization takes place. Just to answer the question,
Now just see app/Mage.php line No.460 you will see

public static function getModel($modelClass = '', $arguments = array())
        return self::getConfig()->getModelInstance($modelClass, $arguments);
So actual initialization of this class happened from getModelInstance method.

see app\code\core\Mage\Core\Model\Config.php line no. 1349

public function getModelInstance($modelClass='', $constructArguments=array())
        $className = $this->getModelClassName($modelClass); // retrieve actual class path
        if (class_exists($className)) {
            $obj = new $className($constructArguments);
            return $obj;
        } else {
            return false;

Advantage of class initializatio throgh getModel() is that all the model classes are being initialized automatically as soon as they called. But the major drawback is that class initialization is tightly coupled.Suppose you want to change make any changes in class initialization code then it will be hard.


Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)