Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

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?

share|improve this question
2  
You got this from twitter didn't you? :) –  Marius Jun 27 at 15:30
1  
@marius you beat me to it. Twitter-questions-as-a-service. –  philwinkle Jun 27 at 15:30
    
@Marius Yeah — trying to encourage the questions I get on twitter to come here instead. –  Alan Storm Jun 27 at 16:55
add comment

1 Answer 1

From a purely coding perspective, if you take a look at the getModelClassName method (a few calls down the stack from Mage::getModel)

public function getModelClassName($modelClass)
{
    $modelClass = trim($modelClass);
    if (strpos($modelClass, '/')===false) {
        return $modelClass;
    }
    return $this->getGroupedClassName('model', $modelClass);
}

you'll see that if Magento doesn't see a / in the class alias, it assumes it's a full class name. However, if the getHelperClassName function

public function getHelperClassName($helperName)
{
    if (strpos($helperName, '/') === false) {
        $helperName .= '/data';
    }
    return $this->getGroupedClassName('helper', $helperName);
}

If Magento doesn't see a / in the class alias, it assumes you're using the short form of

Mage::helper('catalog')

and adds a data to the end of the alias so the class resolves properly (catalog/data to Mage_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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.