I seem to remember some autoloading functionality for classes in d7. Is there any documentation on this?

  • How should one organise the files containing the classes?
  • How can one make sure that parent-classes are loaded on instantiating a child-class?

I would like to avoid writing an autoloader for each module that has Classes.

2 Answers 2


Drupal 7 maintains a code registry and uses it for its classes autoloader. To populate this registry with your module's classes, you should add their files to the files[] entry in it's .info file:

files[] = includes/Foo.inc
files[] = includes/Bar.inc

PHP's class autoloading will make sure that the parent class file is also loaded.

  • .php extension is supported too? E.g: MyClass.class.php? What is Drupal convention here?
    – Codium
    Commented Nov 7, 2015 at 0:38
  • 1
    File extension does not matter, you can also put several classes in a single files. Commented Nov 9, 2015 at 1:53

Yep, Pierre is correct.
Besides that, you can use xautoload module to abbreviate your info file with wildcards.
Doc page: http://drupal.org/node/1976198


files[] = includes/*.inc

And besides that, you can attempt to be forward-compatible with Drupal 8, and use PSR-0 for your modules. This is also supported by xautoload.

Namespace and class: \Drupal\my_module\OptionalSubNamespace\MyClass
File: (module dir)/lib/Drupal/my_module/OptionalSubNamespace/MyClass.php

Doing this will make the files[] stuff obsolete.

  • If you use xautoload, then you can follow PSR-0 and starts using Drupal 8 class and namespace naming conventions and autoloader behavior. Commented Aug 21, 2013 at 14:49
  • that's what i'm saying, isn't it :)
    – donquixote
    Commented Aug 21, 2013 at 15:07
  • is it possible to autoload a class found in a subfolder with xautoload? e.g. in my hook_entity_info, i added 'controller class' => 'Drupal\myentity\controller\MyEntityController' I have put my class in modules/myentity/lib/Drupal/controller/MyEntityController.php but I get an error the controller class could not be loaded.
    – yeahman
    Commented Mar 31, 2014 at 17:34
  • Hi yeahman, can you get on irc #drupal?
    – donquixote
    Commented Mar 31, 2014 at 17:34
  • @yeahman Fact is, I used the scenario you mention a dozen of times. I imagine you simply have a bug in your code.. we can examine on irc.
    – donquixote
    Commented Mar 31, 2014 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.