天天看點

Magento’s Class Instantiation Abstraction and Autoload

PHP Autoload

function __autoload($class_name){

   require('lib/'.strToLower($class_name).'.class.php');

}

Magento Autoload

You cansplit all Magento classes into four parts that we’ll call Namespace, ModuleName, Class Type, and Name.

Namespace_ModuleName_ClassType_Name
           

Directory Structure

Varien’s current recommendation is that all non-core modules be stored in the local folder.      
Namespace/ModuleName/ClassType/Name.php
           

Abstracting Away Class Instantiation

Magento Config Files

For example:

Mage::getModel('dataflow/batch_export');
           
ð                Mage_Dataflow_Model_Batch_Export
           

With your Own Modules

High Concept

The Cart Model has a method named removeItem.          
<!-- ....  -->
           
    <models>
           
        <!-- standard model section -->
           
        <roimagic>
           
            <class>Companyname_RoiMagic_Model</class>
           
        </roimagic>
           
        <!-- new checkout section -->
           
        <checkout>
           
            <rewrite>               
           
                <cart>Companyname_RoiMagic_Model_Cart</cart>
           
            </rewrite>
           
        </checkout>
           
    </models>
           
<!-- ....  -->
           

The<rewrite> node lets Magento know we want to override something in thedefault customer module. Remember, Magento merges all the config filestogether, so it’s seeing something more like

    <class>Mage_Checkout_Model</class>
           
   <resourceModel>checkout_mysql4</resourceModel>          
           
    <rewrite>               
           
        <cart>Companyname_RoiMagic_Model_Cart</cart>
           
    </rewrite>
           
The presence of the <rewrite> node lets Magento know it shouldn’t assume the class you want is          Mage_Checkout_Model_Cart                . that’s          Companyname_RoiMagic_Model_Cart
           

Wrap-up