6

I have the class below:

class Company_Module_Model_Somemodel extends Mage_Core_Model_Abstract
{
    const SOME_CONST = 'constant_text';   
} 

What is the proper and correct way to call the constant in another class? Like this:

$class = Mage::getModel('company_model/somemodel');
echo $class::SOME_CONST;

Or like this:

echo Company_Module_Model_Somemodel::SOME_CONST;

Can you explain why is the one better than other? Thanks!

10
echo Company_Module_Model_Somemodel::SOME_CONST;

This one is way to go because Magento uses an autoloader and your class will be autoloaded by name

For further detail please check the reference link https://stackoverflow.com/questions/16792370/proper-way-to-handle-model-constants-in-magento

I hope this will help

0

You can call the constant as :

class ClassName {
    const CONSTANTNAME = 'ConstantValue';
}

Now you can call this constant as echo ClassName::CONSTANTNAME.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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