Case sensitivity
I recently ran into a problem whilst moving one of my extensions from it’s local environment to a remote staging site. It was a problem that took me an embarrassing amount of time to get to the bottom of. After fruitlessly checking for more complex issues I eventually realised that Mage::getModel was failing to load the model. After a few moments head scratching, I realised that the main different between the two systems was that I work locally on a Mac, and the remote staging server is a *nix distribution. Can you guess what it was yet?
Yes, you (no doubt) guessed correctly, it was the case-sensitive nature of the *nix filesystem. Due to the way Mage::getModel calculates paths for class files, this is something that I’ve never had issues with before. The convention when loading a model seems to be to use a lowercase string for it’s ‘id’, if you look through the core file you will find many lines of code similar to Mage::getModel(‘catalog/product’). During the loading process this string gets passed through – amongst other things – ucwords, this calculates the class name as Mage_Catalog_Model_Product, which in turn get’s loaded by the autoloader using the path app/code/core/Mage/Catalog/Model/Product.php. So where does capitalisation become a problem I hear you cry…
Put simply the problem arises if you name your class in such a way that it contains a capital letter in the middle of a ‘word’, for example StoreLocation (Company_Stores_Model_StoreLocation). There are two easy solution. Firstly you can just leave the capital letter in the string, rather than making it lowercase, making your code Mage::getModel(‘stores/storeLocation’). The second would simply be to makespace your code slightly differently, so that it is a Location class that sits within a Store folder of your module (Company_Stores_Model_Store_Location).
It’s possible to debate the pro’s and con’s of different naming conventions until we’re all long in the tooth, but that’s not the point of this post. I’ll simply leave it with the two simple facts that camel case naming is quite common, it can be found in many places within the Zend Framework, (Zend_Cache_Backend_ZendServer, Zend_Http_CookieJar & Zend_Text_MultiByte are three examples I found by clicking a few random folders), and it can trip you up if you’re not careful.
Cheers.