Web design company India | Custom web development company India

Class Naming Conventions and Their Relationship with the Autoloader in Magento

  • Class Naming Conventions and Their Relationship with the Autoloader in Magento

    As we know Magento was developed based on the Zend Framework, so the rules of class naming in Magento were taken from the Zend Framework. Class Naming Conventions The class names are directly related to a specific path in the file system. For example, let’s takeMage_Page_Helper_Data. This would mean the file exists at app/code/core/Mage/Page/Helper/Data.php. Magento standardizes class names depending on their location in the file system. Such standardization enables automatic class loading (autoloader) instead of using require_once and include_once functions. Rather than the directory separator (‘/’ – invalid character for class names), developers use the underscore character (‘_’). For example, the Mage_Catalog_Model_Product class is located in the /app/code/core/Mage/Catalog/Model/Product.php category. Magento autoloader replaces all underscore characters (‘_’) with the category separator (‘/’) and looks for the file in one of the categories (/app/code/local can be disabled in the app/etc/local.xml in the disable_local_modules tag). - /app/code/core - /app/code/community - /app/code/local - /lib/ File search categories in Magento are defined in app/Mage.php.

     * Set include path
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
    $paths[] = BP . DS . 'lib';
    $appPath = implode(PS, $paths);
    set_include_path($appPath . PS . Mage::registry('original_include_path'));
    include_once "Mage/Core/functions.php";
    include_once "Varien/Autoload.php";

    The part that actually takes the class name and turns it in to the system path can be found in the /lib/Varien/Autoload.php file.The Varien_Autoload class is used for automatic upload:

     * Load class source code
     * @param string $class
    public function autoload($class)
        if ($this->_collectClasses) {
            $this->_arrLoadedClasses[self::$_scope][] = $class;
        if ($this->_isIncludePathDefined) {
            $classFile =  COMPILER_INCLUDE_PATH . DIRECTORY_SEPARATOR . $class;
        } else {
            $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
        $classFile.= '.php';
        //echo $classFile;die();
        return include $classFile;

    Creating objects in Magento

    Magento provides a special method to create models, helpers and blocks, using the Mage global class with a shortened class name. For example, to get the Mage_Catalog_Model_Product model in Magento the Mage :: getModel (‘catalog / product’) method is commonly used. catalog – shortening for Mage_Catalog_Model product – determines which class will be used Correlation of the class name with a shortened name takes place in the module configuration (/etc/config.xml).


  • Post Comments

    Submit Comment

    * Required Fields