Magento: Mage:: Static Functions

I’m sure if you are a Magento developer, you’ve seen or used a lot of these types of methods: Mage::app(), Mage::getModel(), Mage::getSingleton(), etc. I’d like to explain how and why these work if you are unaware.

First, if you open up app/Mage.php, you’ll notice that the class declaration is final class Mage. The ‘final’ php keyword prevents this class from being overridden (extended). Second, you’ll notice that every function in the class is a static function, for instance: public static function getVersion(). A static function basically means that you do not need to instantiate the class/object to use the method.

I will list out some of the main, most useful methods available here. If you want to see the others, just open up app/Mage.php and have a look. You can call these anywhere within your application:

  • Mage::getVersion()
        Returns your current Magento version.
  • Mage::getModel($modelClass = '', $arguments = array())
        Returns a new model object.
  • Mage::helper($name)
        Returns a helper object.
  • Mage::log($message, $level = null, $file = '', $forceLog = false)
        Logging tool. If you only pass in a message, it logs to var/log/system.log, if you have logging enabled.
  • Mage::app()
        Initialize a Magento application object.
  • Mage::getStoreConfig($path, $store = null)
        Returns config value by path (basically anything in System >> Configuration).
  • Mage::getBaseUrl($type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null)
        Returns the base URL by type.
  • Mage::register($key, $value)
        Registers a new variable in the registry.
  • Mage::unregister($key)
        Unregisters a variable in the registry.
  • Mage::registry($key)
        Returns a value from the registry by the given key.
  • Mage::getBaseDir($type = 'base')
        Returns the root absolute path. You can of course change which website by passing in the code.
  • Mage::getModuleDir($type, $moduleName)
        Returns the absolute path of a given module.
  • Mage::getStoreConfigFlag($path, $store = null)
        Returns a config flag by path.
  • Mage::getConfig()
        Returns a config instance.
  • Mage::getUrl($route = '', $params = array())
        Generates a url by route and parameters.
  • Mage::getDesign()
        Returns a design package singleton.
  • Mage::dispatchEvent($name, array $data = array())
        Dispatches an event in Magento.
  • Mage::getSingleton($modelClass = '', $arguments = array())
        Returns a model object singleton.
  • Mage::getResourceModel($modelClass, $arguments = array())
        Returns a resource model object.
  • Mage::throwException($message, $messageStorage = null)
        Throw an exception, the Magento way.
  • Mage::setIsDeveloperMode($mode)
        Turns developer mode on or off ($mode should be true or false).
  • Mage::getIsDeveloperMode()
        Returns if developer mode is enabled or not.
This entry was posted in Magento. Bookmark the permalink.

6 Responses to Magento: Mage:: Static Functions

  1. James says:

    Very useful reaource, thanks. Are you basically referring to factory methods here?

    “A static function basically means that you do not need to instantiate the class/object to use the method.”

    My understanding is that a factory method delegates the implementation to the class itself (in a wider OOP context). So does Magento provide the instantiation of the object within, for example, the product class if the develeoper called mage::getModel(‘catalog/product’)?

    Many thanks

  2. Ajay says:

    very use ful post

  3. im a newbie magento developer. great post!

  4. Pingback: A helpful reference of Magento static methods « ERIK MITCHELL

  5. Pingback: Magento: Mage:: Static Functions | Prattski | Magento & Web … | Magento News

  6. Pingback: Magento: Mage:: Static Functions | Prattski | Magento & Web … | e-commerce News