Drupal 8 Developers Cookbook: Autoloading Classes

An autoloading class is a way of making it easy to use class code without having to know where it lives in your filesystem.  Drupal makes use of this feature of PHP to let you break out your code into an organized set of class files that get used as needed (AKA lazy loading) without you having to tell Drupal anything about them.  As long as the file is named and formatted correctly and stored in the right sub-directory of your module, it will get cataloged and then loaded as needed.

All autoloading class files go into the "/src/" directory under your module's root directory.

  src/ - Autoloader Class Files
    Controller/ - Custom dynamic page controllers
    Form/ - Custom configuration page definitions
    Plugin/ - Custom implementations of common Drupal components
      Block/ - Custom dynamic Drupal block definitions
      Field/ - Custom field formatters
      Filter/ - Custom input filters
      Menu/ - Custom menu items (for dynamically generated items)
      views/ - Custom Views components

Those are directories for some of the more commonly created extensions to Drupal - there are a lot more possibilities.  Just skim through the "/src/" directories of the various core modules to see the wide range of extensions you can create.

Three example file templates are provided in this cookbook for better understanding this system: