Composer API
Class

Composer\Autoload\ClassLoader

class ClassLoader

ClassLoader implements a PSR-0, PSR-4 and classmap class loader.

$loader = new \Composer\Autoload\ClassLoader();

// register classes with namespaces
$loader->add('Symfony\Component', __DIR__.'/component');
$loader->add('Symfony',           __DIR__.'/framework');

// activate the autoloader
$loader->register();

// to enable searching the include path (eg. for PEAR packages)
$loader->setUseIncludePath(true);

In this example, if you try to use a class in the Symfony\Component namespace or one of its children (Symfony\Component\Console for instance), the autoloader will first look for the class under the component/ directory, and it will then fallback to the framework/ directory if not found before giving up.

This class is loosely based on the Symfony UniversalClassLoader.

Methods

getPrefixes()

getPrefixesPsr4()

getFallbackDirs()

getFallbackDirsPsr4()

getClassMap()

addClassMap(array $classMap)

add(string $prefix, array|string $paths, bool $prepend = false)

Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix.

addPsr4(string $prefix, array|string $paths, bool $prepend = false)

Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace.

set(string $prefix, array|string $paths)

Registers a set of PSR-0 directories for a given prefix, replacing any others previously set for this prefix.

setPsr4(string $prefix, array|string $paths)

Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for this namespace.

setUseIncludePath(bool $useIncludePath)

Turns on searching the include path for class files.

bool getUseIncludePath()

Can be used to check if the autoloader uses the include path to check for classes.

setClassMapAuthoritative(bool $classMapAuthoritative)

Turns off searching the prefix and fallback directories for classes that have not been registered with the class map.

bool isClassMapAuthoritative()

Should class lookup fail if not found in the current class map?

setApcuPrefix(string|null $apcuPrefix)

APCu prefix to use to cache found/not-found classes, if the extension is enabled.

string|null getApcuPrefix()

The APCu prefix in use, or null if APCu caching is not enabled.

register(bool $prepend = false)

Registers this instance as an autoloader.

unregister()

Unregisters this instance as an autoloader.

bool|null loadClass(string $class)

Loads the given class or interface.

string|false findFile(string $class)

Finds the path to the file where the class is defined.

Details

at line 60
public getPrefixes()

at line 69
public getPrefixesPsr4()

at line 74
public getFallbackDirs()

at line 79
public getFallbackDirsPsr4()

at line 84
public getClassMap()

at line 92
public addClassMap(array $classMap)

Parameters

array $classMap Class to filename map

at line 109
public add(string $prefix, array|string $paths, bool $prepend = false)

Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix.

Parameters

string $prefix The prefix
array|string $paths The PSR-0 root directories
bool $prepend Whether to prepend the directories

at line 156
public addPsr4(string $prefix, array|string $paths, bool $prepend = false)

Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace.

Parameters

string $prefix The prefix/namespace, with trailing '\'
array|string $paths The PSR-4 base directories
bool $prepend Whether to prepend the directories

Exceptions

InvalidArgumentException

at line 201
public set(string $prefix, array|string $paths)

Registers a set of PSR-0 directories for a given prefix, replacing any others previously set for this prefix.

Parameters

string $prefix The prefix
array|string $paths The PSR-0 base directories

at line 219
public setPsr4(string $prefix, array|string $paths)

Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for this namespace.

Parameters

string $prefix The prefix/namespace, with trailing '\'
array|string $paths The PSR-4 base directories

Exceptions

InvalidArgumentException

at line 238
public setUseIncludePath(bool $useIncludePath)

Turns on searching the include path for class files.

Parameters

bool $useIncludePath

at line 249
public bool getUseIncludePath()

Can be used to check if the autoloader uses the include path to check for classes.

Return Value

bool

at line 260
public setClassMapAuthoritative(bool $classMapAuthoritative)

Turns off searching the prefix and fallback directories for classes that have not been registered with the class map.

Parameters

bool $classMapAuthoritative

at line 270
public bool isClassMapAuthoritative()

Should class lookup fail if not found in the current class map?

Return Value

bool

at line 280
public setApcuPrefix(string|null $apcuPrefix)

APCu prefix to use to cache found/not-found classes, if the extension is enabled.

Parameters

string|null $apcuPrefix

at line 290
public string|null getApcuPrefix()

The APCu prefix in use, or null if APCu caching is not enabled.

Return Value

string|null

at line 300
public register(bool $prepend = false)

Registers this instance as an autoloader.

Parameters

bool $prepend Whether to prepend the autoloader or not

at line 308
public unregister()

Unregisters this instance as an autoloader.

at line 319
public bool|null loadClass(string $class)

Loads the given class or interface.

Parameters

string $class The name of the class

Return Value

bool|null True if loaded, null otherwise

at line 335
public string|false findFile(string $class)

Finds the path to the file where the class is defined.

Parameters

string $class The name of the class

Return Value

string|false The path if found, false otherwise