To start, Magento was created using the Zend framework. Therefore, the naming conventions will pretty much follow the rules established by Zend. A quick review of the naming conventions in the Zend framework is as follows:
- All framework classes are stored under a root directory
- Class names may only contain alphanumeric characters
- Numbers are allowed in class names, but discouraged
- Underscores are allowed, however, they are only permitted in place of a path separator
- If the class name has more than one word, each word’s first letter must be capitalized
- Code that is not a part of the standard or extras libraries, but, is being deployed next to the Zend Framework must never start with Zend_ or ZendX_
- Abstract classes are the same as the other classes, but, they must end in _Abstract
- Interfaces follow the same rules as other classes, but, it’s encouraged but not required to end the name with _Interface
Now that we have some general rules, let’s take a look at the autoloader located 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";
…You’ll notice that the first four lines show the Magento loading hierarchy that we’re always talking about.
Above, when talking about the Zend naming rules, this rule is very important in the relationship with the Magento autoloader: Underscores are allowed, however, they are only permitted in place of a path separator. The naming of classes is standardized based on the location in the file system. If we take a look in the Magento core for an example, we can see how this works. I’ve chosen to take a peek at the file located here: /app/core/Mage/Authorizenet/controllers/Directpost/PaymentController.php. Here is the line we’re worried about:
class Mage_Authorizenet_Directpost_PaymentController extends Mage_Core_Controller_Front_Action
Let’s go through the rules:
- It’s stored under a root directory
- It’s alphanumeric
- No numbers
- Underscores… it’s right, because, it’s located in the
Mage/Aurthorizenet/Directpost/directory, and, the file is namedPaymentController.php… so, we replace the/with_and that’s our class name. - It has more than one word, so, each word is capitalized.
- It IS a part of the system, so, no worries there
- It’s NOT an abstract class
- It’s NOT an interface
So, since everything is named properly, the Magento autoloader will pick up the underscores, and, know the location, and, everything will run along smoothly. This madness of standardization allows the Magento autoloader to do all the dirty work of including (loading, autoloading) the classes thereby eliminating the need for us to use include_once or require_once.