3

Example scenario:

$ grep ^class app/code/local/MyApp/MadCaseExample/Block/CaseMaker/BlockThing.php
class MyApp_MadCaseExample_Block_CaseMaker_BlockThing extends MyApp_MadCaseExample_Block_Mce
$ grep -iF 'blockthing' app/design/frontend/myapp/default/layout/madcaseexample.xml
<block type="madcaseexample/casemaker_blockthing" name="madcaseexample.pages.blockthing" template="…"/>

The above won't work because Magento doesn't magically transform the xml names to the right case to find the classfile. An implicit lowercase convention seems to exist, so for a while we've been (I think cargo-culting) our classfiles this way:

app/code/local/MyApp/Madcaseexample/Block/Casemaker/Blockthing.php

Even though we really want to use camel case for most of our classnames. But would it really be so wrong to just be case-sensitive in the XML as well?

<block type="MadCaseExample/CaseMaker_BlockThing" …

What's the benefit of having to express the file and classnames two or three different ways?

8

So let me first explain how Magento turns class aliases into objects. It's all defined in Mage_Core_Model_Config, if you want to have a look. The key function is getGroupedClassName, which in this case will be called with 'block' as its first argument.

The first part of the alias ('madecaseexample') is matched in the config xml, and this is case sensitive because xml is case sensitive, and so is SimpleXML, and ergo so is Magento.

The second part concatenated with the result of the lookup to the config node. The autoloader works by replacing all of the underscores with spaces, and running uc_words to turn the first letter after every '_' into a capital. Note that if they are already capitalised, it will do nothing. It then takes that string and requires it, and looks for the class name.

The reason you need to camel case your classes is so that Magento can find the class name correctly, because it doesn't use grep, it uses a deterministic algorithm.

If it looked things up without case sensitivity, you would take a performance hit, because case insensitive grepping through directories is hard, and would require a lot more hard drive operations. Furthermore, there would not be a one to one relationship between class aliases and classes, which would rather defeat the point of class aliases.

As an example, without case sensitivity, 'madcaseexample/casemaker_blockthing' could reference MyApp/Madcaseexample/Block/Casemaker/Blockthing.php, or it could reference MyApp/Madcaseexample/Block/CaSeMaker/BlockthIng.php, and they could be different things.

If you follow a convention, then you can ensure that there is one to one correspondence between class aliases, directories and class names, which solves all your problems, because you know that one form can only reference exactly one other.

For example, I would write your class alias as madecaseexample/caseMaker_blockThing, but you could choose anything, as long as it's consistent.

  • 1
    Well-said. Where I see this get very confusing for new(er) Magento devs is when they are/their team is developing on a case-insensitive filesystem (Windows & standard OSX). Case issues don't rear their head till deploying to the typical case-sensitive production OS. – benmarks Aug 30 '13 at 14:04
  • Thanks. It was actually you who taught me how the autoloader worked in your video series. – Max Bucknell Aug 30 '13 at 14:18
  • I thought your explanation sounded very smart & scholarly :-D – benmarks Aug 30 '13 at 15:31
  • Well thank you very much. I'm interning at a digital agency this summer, and I'm picking it up. I think I'll stick at it though. In no small part due to the clarity of your explanations. Magento makes sense, and it's good. – Max Bucknell Aug 30 '13 at 15:48
  • One thing that greatly helped me understand the autoloader was to reimplement Magento's method in a standalone app. I built a basic front-end framework that uses Magento's patterns, and when I wrote the autoloader (quite similar but slightly different than Magento) I gained a lot of understanding about how it works. It's quite simple, really. – pspahn Aug 31 '13 at 13:16

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.