The Composer ClassLoader is used to handle any class loading. This allows loading of any Drupal classes or any third-party vendor libraries.
API changes
Autoloading
Composer provides an autoload.php file which registers its class loader and all required namespaces. If you are manually bootstrapping Drupal, ensure this file is included beforehand so that the classes are available:
Drupal 7
require_once 'drupal/core/includes/bootstrap.inc';
Drupal 8
require_once 'drupal/core/vendor/autoload.php';
require_once 'drupal/core/includes/bootstrap.inc';
Interacting with the Class Loader
In most cases, class namespaces are already registered for you and you should not have to worry about registering the class namespaces yourself. There might be times, however, where you need direct access to the class loader object itself. The class loader instance is available by calling the drupal_classloader()
function. By calling this function, you have access to register additional namespaces, interact with its class map, etc. This is a Composer\Autoload\ClassLoader object.
Drupal 7
In Drupal 7, there are a number of contributed modules that allow interacting with a class loader. Some of these include ClassLoader, X Autoload, etc.
Drupal 8
$loader = drupal_classloader();
$loader->add('MyNamespace', __DIR__);