Magento Static Functions



When you open up app/Mage.php, you’ll see that the class declaration of Mage is final. The ‘final’ keyword prevents this class from being overridden. And you’ll see that every function is a magento static function, for example: public static function getVersion(). A static function means that you do not need to instantiate the class to use the method.

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

Mage::getVersion();

It will return your current Magento version.

Mage::getModel($modelClass = '', $arguments = array());

It returns a new model object.

Mage::app();

It will Initialize a Magento application object.

Mage::getBaseUrl($type = Mage_Core_Model_Store::URL_TYPE_LINK, $secure = null); 

Returns the base URL by type.

Mage::register($key, $value); 

It registers a new variable in the registry.

Mage::unregister($key); 

It unregisters a variable in the registry.

Mage::registry($key);

It returns a value from the registry by the given key.

Mage::getBaseDir($type = 'base');

It returns the root absolute path.

Mage::getModuleDir($type, $moduleName); 

It returns absolute path of a given module.

Mage::getConfig(); 

Returns a config instance.

Mage::getUrl($route = '', $params = array()); 

It generates a url by route and parameters.

Mage::getStoreConfigFlag($path, $store = null); 

It returns a config flag by path.

Mage::dispatchEvent($name, array $data = array());

Dispatches an event in Magento.

Mage::getSingleton($modelClass = '', $arguments = array()); 

It returns a model object singleton.

Mage::getResourceModel($modelClass, $arguments = array()); 

it returns a resource model object.

Mage::throwException($message, $messageStorage = null);

Throw an exception in Magento way.

Mage::helper($name); 

it returns a helper object.

Mage::log($message, $level = null, $file = '', $forceLog = false);

It’s a logging tool. If you only pass a message, it logs to var/log/system.log, if you have logging enabled.