Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Mage::app and Mage:: in Magento

Tags:

magento

Can any body say what is the difference between Mage:app and Mage::

eg:

Mage::getModel('catalog/product');

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

if i try like this

Mage::app->getModel('catalog/product');

it throws a fatal error

What is the basic difference?

like image 818
zamil Avatar asked Nov 28 '22 14:11

zamil


2 Answers

As a more general explanation and further to Alan's answer Mage::app() and Mage::getModel() are both static methods of the Mage class (app/Mage.php).

The Mage::app() function is used to bootstrap your Magento application (setting up configuration, autoloading etc) and is useful when wanting to access Magento models in your own custom script for example.

It is a static method so it can also be called at any point in your application to return the current Mage_Core_Model_App object which you can use for example to get current configuration values e.g. Mage::app()->getStore() will return the current Mage_Core_Model_Store store object.

Mage::app() is similar to Mage::run() found in your index.php file. The difference is that Mage::run() will amongst other things also invoke the MVC, routing etc part of your application as per default and control the request/response directing you to a page and instantiating the blocks and layout template rendering.

Mage::getModel() is simply a factory method that will return a new instance of a class based on the class alias you provide. For example Mage::getModel('customer/customer') will return a new Mage_Customer_Model_Customer object. If you want the same object back each time throughout your request you can use Mage::getSingleton() instead.

like image 186
drj201 Avatar answered Nov 30 '22 04:11

drj201


When you say

Mage::getModel('catalog/product');

You're calling the static method getModel on the class Mage

#File: app/Mage.php
public static function getModel($modelClass = '', $arguments = array())
{
    return self::getConfig()->getModelInstance($modelClass, $arguments);
}

When you say

Mage::app->getModel('catalog/product');

you're going to get a fatal error, because that's invalid PHP.

However, if you say

$app = Mage::app();

You're calling the static method app on the Mage class

public static function app($code = '', $type = 'store', $options = array())
{
    if (null === self::$_app) {
        self::$_app = new Mage_Core_Model_App();
        self::setRoot();
        self::$_events = new Varien_Event_Collection();
        self::$_config = new Mage_Core_Model_Config();

        Varien_Profiler::start('self::app::init');
        self::$_app->init($code, $type, $options);
        Varien_Profiler::stop('self::app::init');
        self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
    }
    return self::$_app;
}

which will return a Mage_Core_Model_App object, which will let you call any method on the class/object at

app/code/core/Mage/Model/App.php
like image 40
Alan Storm Avatar answered Nov 30 '22 03:11

Alan Storm