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() {
$this->_init('cutehits_employee/statusreport');
}

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

Mage::getModel('cutehits_employee/statusreport')->ModelMethod1();

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

<models>
            <cutehits_employee>
                <class>Cutehits_Employee_Model</class>
                <resourceModel>cutehits_employee_mysql4</resourceModel>
            </cutehits_employee>
<models>

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)) {
            Varien_Profiler::start('CORE::create_object_of::'.$className);
            $obj = new $className($constructArguments);
            Varien_Profiler::stop('CORE::create_object_of::'.$className);
            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

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)