PHP namespaces in Drupal (and autoloading)

Your server must be running at least PHP5.3 to handle namespaces !
The conventions for autoloading with namespaces are different between Drupal7 and Drupal8 !

From Drupal: namespaces:

Modules creating classes should place their code inside a custom namespace. The convention for those namespaces is

namespace Drupal\example_module

To support autoloading, these classes should be placed inside the correct folder:

<module folder> /lib/ <namespace>

But as remarked at A little confused w/ Namespaces & AutoLoading:

The last paragraph ... conflicts w/ the examples of modules declaring their classes in the .info file.

How are we supposed to store class files using namespaces, and the advantages of autoload?

For example, Commerce declares in

files[] = includes/views/handlers/

seemingly creating class files arbitrarily in the includes folder.

Applies only to D8
Posted by mikeejt on March 8, 2013 at 10:15am

After doing some research, found that the last paragraph above only applies to Drupal 8.

According to this comment by Crell: Drupal's autoloader in D7 will be replaced by Symfony's autoloader, which uses php 5.3's PSR-0 autoloader.

As Drupal modernizes more to OOP, the use of namespaces will help avoid class name clashes; but to take advantage of Symfony's autoloader, class files need to be created in the correct structure.

Hat tip to for helping me understand Drupal's (D7 & D8) autoloader works.

Visit also