Class not dynamically loaded in custom module I have a very simple Drupal 7 module in which I want to load a custom class. I'm not able to load the class and I get the following error when I enable the module:

Fatal error: Class 'Drupal\testmodule\TestClass' not found in www/sites/all/modules/testmodule/testmodule.module on line 4


Here are the contents of my module files:

testmodule.info

name = "Test Module"
core = 7.x
files[] = src/TestClass.php


testmodule.module

&lt;?php

function testmodule_init() {
  Drupal\testmodule\TestClass::go();
}


src/TestClass.php

&lt;?php

namespace Drupal\testmodule;

class TestClass {
  function go() {
    print 'Foo!';
  } 
}


Please could anyone help me understand why Drupal 7's dynamic-loading code registry is not working as documented here: https://www.drupal.org/docs/7/creating-custom-modules/writing-module-info-files-drupal-7x#files ?
To use namespaced classes in Drupal 7, you can use one of these modules:


https://drupal.org/project/xautoload
https://drupal.org/project/registry_autoload


One reason for choosing one or the other may be if you have other modules that also depend on one of those modules.

Otherwise I guess it is largely a matter of taste.

One difference is that xautoload is more convenient during development, because newly created PSR-4 classes are immediately available without rebuilding the class registry.

On the other hand, it does a lot of things which you probably don't need right now, and the code looks complex.

Of course you could do some profiling to find out which is faster, but as always you would need to be careful what you measure and what you compare. I currently cannot tell you which one is faster, or better for memory.

There is also the alternatives module that can help to have only one of those enabled, instead of both.

Disclaimer: I am the author of one of these modules, and I am subscribed to the autoload tag on drupal.stackexchange.com.