0

I just spent a good couple of hours trying to figure out why the heck my simple test module seemed to be recognised by Magento but not working. It turns out it was because I had named it aXeHeadCoding_ObserverTest, and some part of the module system doesn't like the lowercase first letter. I don't know whether it's the config XML or the name of the directory, but it really screwed me over.

Now I'm curious. Is there a known reason for requiring a starting capital letter? And/or which part of the system is it that requires the capital letter? (I realise I could find out myself by simply changing each instance of AXeHeadCoding to aXeHeadCoding until it breaks, but trial and error isn't my favourite way to determine this sort of thing)

Clonkex
  • 109
  • 6

1 Answers1

1

If you take a look at file lib/Varien/Autoloader.php you will find a function called autoload($class).

The important block of code which is requiring to start namespace with an uppercase word is:

(...)
} else {
        $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
}

As you can see the class name here is changed from Namespace_Module_Model_Class to path: Namespace/Module/Model/Class. Because of the ucwords even if your class name would be nameSpace_module_Model_Class the autoloader would try to include file from path: NameSpace/Module/Model/Class.

Take a look at this answer too:

What is the benefit of the letter case conventions in layout xml?

versedi
  • 2,059
  • 19
  • 35
  • Aha, thank you! This explains it very nicely. As a Magento noob, I had no idea what Autoloader.php was (and still don't really) so I didn't even know where to look to find how this worked. – Clonkex May 11 '17 at 06:48
  • 1
    An Autoloader is not specific to Magento, but is widely used in PHP, more detailed info here: http://php.net/manual/en/language.oop5.autoload.php – Niels May 11 '17 at 07:36
  • 1
    @mizuti Huh, thanks for the info. I didn't know that. Personally that system doesn't appeal to me because I greatly prefer if things are explicitly written in code rather than magically happening in the background (and it definitely makes it a lot more difficult for newbies to learn), but that opinion will probably change sometime in the future. – Clonkex May 11 '17 at 23:04
  • 1
    @Clonkex I understand your perspective, but believe me, if you'll ever start working on larger scale applications, you'll see that it makes your life a lot easier, for example because it is much easier to include third party code if you stick to the correct include path and namespaces. Personally, I don' think it is more difficult to learn, once you get the hang of it you want to use it in every project you work on. – Niels May 12 '17 at 06:38
  • 1
    @mizuti Perhaps, but the thing I find extremely difficult while trying to learn Magento is that there's no way to differentiate between things you can name whatever you want and things that must be named a particular way to make everything work. For example, when you're teaching someone to program, you explain that a variable can be named whatever you want, and that name is used to refer to it later, but keywords must be typed the same way every time. With Magento, everything is in the config XMLs so it's all just text with no clear division between name and keyword, so to speak. – Clonkex May 12 '17 at 13:02