Documentation

ClassLoader
in package

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.

Tags
author

Fabien Potencier fabien@symfony.com

author

Jordi Boggiano j.boggiano@seld.be

see
https://www.php-fig.org/psr/psr-0/
see
https://www.php-fig.org/psr/psr-4/

Table of Contents

$apcuPrefix  : string|null
$classMap  : array<string|int, string>
$classMapAuthoritative  : bool
$fallbackDirsPsr0  : array<string|int, array<string|int, mixed>>
$fallbackDirsPsr4  : array<string|int, array<string|int, mixed>>
$missingClasses  : array<string|int, bool>
$prefixDirsPsr4  : array<string|int, array<string|int, mixed>>
$prefixesPsr0  : array<string|int, array<string|int, mixed>>
$prefixLengthsPsr4  : array<string|int, array<string|int, mixed>>
$registeredLoaders  : array<string|int, self>
$useIncludePath  : bool
$vendorDir  : string|null
__construct()  : mixed
add()  : void
Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix.
addClassMap()  : void
addPsr4()  : void
Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace.
findFile()  : string|false
Finds the path to the file where the class is defined.
getApcuPrefix()  : string|null
The APCu prefix in use, or null if APCu caching is not enabled.
getClassMap()  : array<string|int, string>
getFallbackDirs()  : array<string|int, array<string|int, mixed>>
getFallbackDirsPsr4()  : array<string|int, array<string|int, mixed>>
getPrefixes()  : array<string|int, string>
getPrefixesPsr4()  : array<string|int, array<string|int, mixed>>
getRegisteredLoaders()  : array<string|int, self>
Returns the currently registered loaders indexed by their corresponding vendor directories.
getUseIncludePath()  : bool
Can be used to check if the autoloader uses the include path to check for classes.
isClassMapAuthoritative()  : bool
Should class lookup fail if not found in the current class map?
loadClass()  : true|null
Loads the given class or interface.
register()  : void
Registers this instance as an autoloader.
set()  : void
Registers a set of PSR-0 directories for a given prefix, replacing any others previously set for this prefix.
setApcuPrefix()  : void
APCu prefix to use to cache found/not-found classes, if the extension is enabled.
setClassMapAuthoritative()  : void
Turns off searching the prefix and fallback directories for classes that have not been registered with the class map.
setPsr4()  : void
Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for this namespace.
setUseIncludePath()  : void
Turns on searching the include path for class files.
unregister()  : void
Unregisters this instance as an autoloader.
findFileWithExtension()  : string|false

Properties

$classMap

private array<string|int, string> $classMap = array()
Tags
psalm-var

array<string, string>

$classMapAuthoritative

private bool $classMapAuthoritative = false

$fallbackDirsPsr0

private array<string|int, array<string|int, mixed>> $fallbackDirsPsr0 = array()
Tags
psalm-var

array<string, string>

$fallbackDirsPsr4

private array<string|int, array<string|int, mixed>> $fallbackDirsPsr4 = array()
Tags
psalm-var

array<string, string>

$missingClasses

private array<string|int, bool> $missingClasses = array()
Tags
psalm-var

array<string, bool>

$prefixDirsPsr4

private array<string|int, array<string|int, mixed>> $prefixDirsPsr4 = array()
Tags
psalm-var

array<string, array<int, string>>

$prefixesPsr0

private array<string|int, array<string|int, mixed>> $prefixesPsr0 = array()
Tags
psalm-var

array<string, array<string, string[]>>

$prefixLengthsPsr4

private array<string|int, array<string|int, mixed>> $prefixLengthsPsr4 = array()
Tags
psalm-var

array<string, array<string, int>>

$registeredLoaders

private static array<string|int, self> $registeredLoaders = array()

$useIncludePath

private bool $useIncludePath = false

Methods

__construct()

public __construct([string|null $vendorDir = null ]) : mixed
Parameters
$vendorDir : string|null = null
Return values
mixed

add()

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

public add(string $prefix, array<string|int, string>|string $paths[, bool $prepend = false ]) : void
Parameters
$prefix : string

The prefix

$paths : array<string|int, string>|string

The PSR-0 root directories

$prepend : bool = false

Whether to prepend the directories

Return values
void

addClassMap()

public addClassMap(array<string|int, string> $classMap) : void
Parameters
$classMap : array<string|int, string>

Class to filename map

Tags
psalm-param

array<string, string> $classMap

Return values
void

addPsr4()

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

public addPsr4(string $prefix, array<string|int, string>|string $paths[, bool $prepend = false ]) : void
Parameters
$prefix : string

The prefix/namespace, with trailing '\'

$paths : array<string|int, string>|string

The PSR-4 base directories

$prepend : bool = false

Whether to prepend the directories

Tags
throws
InvalidArgumentException
Return values
void

findFile()

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

public findFile(string $class) : string|false
Parameters
$class : string

The name of the class

Return values
string|false

The path if found, false otherwise

getApcuPrefix()

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

public getApcuPrefix() : string|null
Return values
string|null

getClassMap()

public getClassMap() : array<string|int, string>
Tags
psalm-return

array<string, string>

Return values
array<string|int, string>

Array of classname => path

getFallbackDirs()

public getFallbackDirs() : array<string|int, array<string|int, mixed>>
Tags
psalm-return

array<string, string>

Return values
array<string|int, array<string|int, mixed>>

getFallbackDirsPsr4()

public getFallbackDirsPsr4() : array<string|int, array<string|int, mixed>>
Tags
psalm-return

array<string, string>

Return values
array<string|int, array<string|int, mixed>>

getPrefixes()

public getPrefixes() : array<string|int, string>
Return values
array<string|int, string>

getPrefixesPsr4()

public getPrefixesPsr4() : array<string|int, array<string|int, mixed>>
Tags
psalm-return

array<string, array<int, string>>

Return values
array<string|int, array<string|int, mixed>>

getRegisteredLoaders()

Returns the currently registered loaders indexed by their corresponding vendor directories.

public static getRegisteredLoaders() : array<string|int, self>
Return values
array<string|int, self>

getUseIncludePath()

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

public getUseIncludePath() : bool
Return values
bool

isClassMapAuthoritative()

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

public isClassMapAuthoritative() : bool
Return values
bool

loadClass()

Loads the given class or interface.

public loadClass(string $class) : true|null
Parameters
$class : string

The name of the class

Return values
true|null

True if loaded, null otherwise

register()

Registers this instance as an autoloader.

public register([bool $prepend = false ]) : void
Parameters
$prepend : bool = false

Whether to prepend the autoloader or not

Return values
void

set()

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

public set(string $prefix, array<string|int, string>|string $paths) : void
Parameters
$prefix : string

The prefix

$paths : array<string|int, string>|string

The PSR-0 base directories

Return values
void

setApcuPrefix()

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

public setApcuPrefix(string|null $apcuPrefix) : void
Parameters
$apcuPrefix : string|null
Return values
void

setClassMapAuthoritative()

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

public setClassMapAuthoritative(bool $classMapAuthoritative) : void
Parameters
$classMapAuthoritative : bool
Return values
void

setPsr4()

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

public setPsr4(string $prefix, array<string|int, string>|string $paths) : void
Parameters
$prefix : string

The prefix/namespace, with trailing '\'

$paths : array<string|int, string>|string

The PSR-4 base directories

Tags
throws
InvalidArgumentException
Return values
void

setUseIncludePath()

Turns on searching the include path for class files.

public setUseIncludePath(bool $useIncludePath) : void
Parameters
$useIncludePath : bool
Return values
void

unregister()

Unregisters this instance as an autoloader.

public unregister() : void
Return values
void

findFileWithExtension()

private findFileWithExtension(string $class, string $ext) : string|false
Parameters
$class : string
$ext : string
Return values
string|false

Search results