June 10, 2015

In this article, we are going to discuss about the Functional and Factory class groups in Magento. As you know Magento is built based on module architecture, which leads to the requirement that there must be an interaction between modules. Hence, in this part we will learn about the way these modules used.

Definition and Examples of Functional and Factory class groups

Functional Class
  •     Class: only contains functions and static attributes? (not sure)
  •     For example: Mage

Factory Class

  •     Class: consists of functions to create the instance (object) of different Classes. Class depends on input parameters
  •     For example: class Mage

Create an instance of class Mage_Catalog_Model_Product

Mage::getModel('catalog/product')

Generate an instance of class Mage_Catalog_Block_Product_View

Mage::getBlockSingleton('catalog/product_view')

Definition of Instance, the ways to create the instance object in Magento

  • Definition : In OOP, Instance is an Object
  • Create an instance object in Magento

Mage::getModel('catalog/product');
Mage::getBlockSingleton('catalog/product_view');
Mage::app()->getLayout()-createBlock('catalog/product_view')

The process to generate an instance through the function Mage::getModel() is as below:

1) Call function getModel() trong class Mage

2) Call function getModelInstance() in class Mage_Core_Model_Config

3) Call function getModelClassName(). This function will return the name of the model with catalog/product is Mage_Catalog_Model_Product.

4) Add a new object by the New command:

$obj = new $className($constructArguments);

In this example, $className = 'Mage_Catalog_Model_Product'

Get different instances from different places:

– With creating a Instance of a model, the function Mage::getModel() always returns a new object (instance).

– Function Mage::getSingleton() always gives only one object (instance) back.

0 comments:

Post a Comment