Mage_Catalog_Model_Product_TYPE::TYPE_CONFIGURABLE vs 'configurable'
-
13-12-2019 - |
Question
I'm working on an heavy customised magento version. Cancelling an order returned this fatal error:
Mage_Catalog_Model_Product_TYPE not found
on this line code:
if($parent->getTypeId() == Mage_Catalog_Model_Product_TYPE::TYPE_CONFIGURABLE)
I fixed changing it to
if($parent->getTypeId() == 'configurable')
Can you please tell me what's the difference between them and if my solution can lead to other issues? many thanks
Solution
Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
(notice the capitalization, which was the source of your error) is a constant. Using a constant for values that won't change is preferred over writing "magic strings"1 all over the application.
Why?
- no errors due to typos. If you misspell the string, chances are, you won't notice until much later. If you reference a non existent constant, the compiler complains immediately (or with PHP, your IDE or static code analysis tools like CodeSniffer)
- easier to refactor
- more context, thus easier to understand. It is clear, where the value comes from and what purpose it serves
1) Read this: https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad - it is about magic numbers, but magic strings are almost as bad
OTHER TIPS
It should be Mage_Catalog_Model_Product_Type
and not Mage_Catalog_Model_Product_TYPE
(note the capitalisation on the last word)