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.

 

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 Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...