Get Free Audit

Describing Class Group Configuration and Use in Factory Methods (Magento Certified Developer Exam)

Jun 19, 2012

960 Sergei Guk

Describing Class Group Configuration and Use in Factory Methods (Magento Certified Developer Exam)

We suppose you continue following our Magento certification-dedicated posts in our blog. This time we will describe class group configuration and use in factory methods.

Magento uses factory methods to instantiate Models, Blocks and Helpers classes, applying a necessary method (for example getModel, helper etc.). You should pass an abstract name of a class group, followed by an entity name. Class groups are described in configuration XML files in /etc/config.xml files of appropriate modules.

Let’s have a look at the proccess of class instantiating by the example of models and try to instantiate the product object. This can be done by using getModel() method:

$product = Mage::getModel(‘catalog/product’);


This method retrieves an instance of Mage_Catalog_Model_Product this way:
app/Mage.php


getModel() calls getModelInstance() method, which gets class instance with the help of getModelClassName

Magento store.belvg.com

Magento Extension

Take your online store to the next level with BelVG Magento Extension

Visit the store

app/code/core/Mage/Core/Model/Config.php

getGroupedClassName() method does all the work. As you can see from getModelClassName, we pass the group type (model, block or helper) and the class identifier (in our case catalog/product) to this method. It explodes our string by using ‘/’ as needle, removes whitespaces and forms array of strings.

Then we load Varien_Simplexml_Element and pass our group name (catalog) to find class prefix name from config.xml of our module. In our example it would be Mage_Catalog_Model; in case this extension was overwritten, it could be Namespace_Catalog_Model. We also add entity class name (product) and after using Magento uc_words (based on PHP ucwords that returns a string with the first character of each word capitalized, if that character is alphabetic), we finally receive Mage_Catalog_Model_Product that will be returned in getModelInstance. As we noticed from above, it will create the object, using a new operator.

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Now let’s review how to declare class alias – a string that calls class (model, block, helper – depends from usage context)) we have declared. Alias naming rule is: group/entity.

As we observed, the group name is declared in configuration files in /etc/folder of the module.

General case

In our specific case

Creating models objects, we can also try to use Mage::getSingleton() method. The method differs from getModel() by using different Design Pattern called Singleton, which means that only one instantiation of a class is used during the runtime process. However, when calling getModel(), a new object will be created every time the method is called.

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Helpers

Instantiate a helper class the same way you do models. We call Mage::helper(‘group/entity’) and it calls getHelperClassName() in its turn, which has a default value ‘data’ in entity name. It means that if you pass only a group name, for example Mage::helper(‘catalog’), it will create an object of Mage_Catalog_Helper_Data class.


From the code above you may notice that Magento registers a variable when the helper is being created. So helper instance can be created only one time during runtime.



Blocks

The proccess of instantiating blocks is similar to the one of creating models and helpers. Blocks can be instantiated via layout xml (

P.S. In Magento 2.0 this process will be changed as follows: Support to receive and process direct class names was added to all basic Magento methods that create objects. Support for class-id strings was retained to ensure a smooth transition (Framework Changes from 1.x to 2.x).

So instead of Mage::getModel(‘catalog/product’) it will be Mage::getModel(‘Mage_Catalog_Model_Product’). But this would be a completely new story;)

 

Read our previous posts

Explaining how Magento loads and manipulates configuration information. Part II

Explaining how Magento loads and manipulates configuration information. Part I

Class Naming Conventions and Their Relationship with the Autoloader

The Main Magento Design Areas and More

Magento Module Structure

Magento Codepools

magento custom development

Magento Custom Development

Take your online store to the next level with BelVG Magento Custom Development

Visit the page
Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

6 Comments

  1. Haha, thanks for the comment Marijus :) Shame on me for such a silly mistake

  2. The very last sentence of “Models” section says:
    “However, when calling getModel(), a new class will be created every time the method is called.”

    As far as I have encountered, the very first lesson in OOP is always the definition of what “class” is and what “object”/”instance” is and the distinction between these concepts. It’s highly astonishing that developers with 5-8 years of experience still happen to mix those! Just as surprising is that no one in almost a year had any problem with it :)

    Keep up the good work!

  3. Sergei, these are very good posts and helpful for certification.
    As Milen pointed out class name errors in this post, which you are aware of, you should correct it.

  4. Hi Sergei,

    I’m preparing for MCD and these tutorials are really helpful. Thanks for these great tutorials.

    Happy MCD to all who are preparing for MCD.

    Best Regards,
    Shailendra Khare

  5. Hi Milen,

    Thanks for mentioning this typing error, my mistake:) Really glad that someone reads these articles carefully. Will try to post something new at least once in a week.

  6. I do not think you are correct. For helpers the class name must be Namespace_Modulename_Helper and for blocks it must be Namespace_Modulename_Block.
    Anyway your posts are very good and useful and I would love to see them more frequently, as with this speed until you cover all the topics Magento 2 will be already available and widely used ;)
    Regards

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *