In Magento 1, if I use the full Magento class name in a factory method, I'm able to instantiate an object
//trying full class name instead of catalog/product
$object = Mage::getModel('Mage_Catalog_Model_Product');
However, the same thing won't work for helpers. If you try
Mage::helper('Mage_Core_Helper_Url');
You get
Warning: include(Mage/Mage/Core/Helper/Url/Helper/Data.php): failed to open stream: No such file or directory in /path/to/magentolib/Varien/Autoload.php on line 93
#0 /path/to/magentolib/Varien/Autoload.php(93): mageCoreErrorHandler(2, 'include(Mage/Ma...', '/path/to/magent...', 93, Array)
#1 /path/to/magentolib/Varien/Autoload.php(93): Varien_Autoload::autoload()
#2 [internal function]: Varien_Autoload->autoload('Mage_Mage_Core_...')
#3 /path/to/magentoapp/Mage.php(547): spl_autoload_call('Mage_Mage_Core_...')
#4 /path/to/magentoapp/code/local/Sebastianjuffar/Commercebug/controllers/IndexController.php(11): Mage::helper('Mage_Core_Helpe...')
#5 /path/to/magentoapp/code/core/Mage/Core/Controller/Varien/Action.php(418): Sebastianjuffar_Commercebug_IndexController->indexAction()
#6 /path/to/magentoapp/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#7 /path/to/magentoapp/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#8 /path/to/magentoapp/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#9 /path/to/magentoapp/Mage.php(684): Mage_Core_Model_App->run(Array)
#10 /path/to/magentoindex.php(87): Mage::run('', 'store')
#11 {main}
What's going on?
Best Answer
From a purely coding perspective, if you take a look at the
getModelClassName
method (a few calls down the stack fromMage::getModel
)you'll see that if Magento doesn't see a
/
in the class alias, it assumes it's a full class name. However, if thegetHelperClassName
functionIf Magento doesn't see a
/
in the class alias, it assumes you're using the short form ofand adds a
data
to the end of the alias so the class resolves properly (catalog/data
toMage_Catalog_Model_Data
).This enables the short form helpers, but makes it impossible for Magento to tell the difference between a short form helper alias and a long form class name.
The ultimate "why" of this is probably hard to pin down — that the full class name instantiation works like that at all may just be a side effect of protective coding practices from one developer that were incompatible with another developer's desire for each module to have a "main" helper class. It also might be a single overworked developer making quick decisions as they go. There's probably lesson in project management and systems development in there somewhere.