Magento makes use of factory methods to instantiate Model
, Helper
, and Block
classes. A factory method is a design pattern that allows us to instantiate an object without using the exact class name and using a class alias instead.
Magento implements several factory methods, as follows:
Each of these methods takes a class alias that is used to determine the real class name of the object that we are trying to instantiate; for example, if we wanted to instantiate a product
object, we can do so by calling the getModel()
method:
$product = Mage::getModel('catalog/product');
Notice that we are passing a factory name composed of group_classname/model_name
; Magento will resolve this to the actual class name of Mage_Catalog_Model_Product
. Let's take a closer look at the inner workings of getModel()
:
public static function getModel...