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, string>
$classMapAuthoritative  : bool
$fallbackDirsPsr0  : array<int, string>
$fallbackDirsPsr4  : array<int, string>
$includeFile  : callable
$missingClasses  : array<string, bool>
$prefixDirsPsr4  : array<string, array<int, string>>
$prefixesPsr0  : array<string, array<string, array<int, string>>>
List of PSR-0 prefixes
$prefixLengthsPsr4  : array<string, array<string, int>>
$registeredLoaders  : array<string, 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, string>
getFallbackDirs()  : array<int, string>
getFallbackDirsPsr4()  : array<int, string>
getPrefixes()  : array<string, array<int, string>>
getPrefixesPsr4()  : array<string, array<int, string>>
getRegisteredLoaders()  : array<string, self>
Returns the currently registered loaders keyed 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
initializeIncludeClosure()  : void

Properties

$classMap

private array<string, string> $classMap = array()

$classMapAuthoritative

private bool $classMapAuthoritative = false

$fallbackDirsPsr0

private array<int, string> $fallbackDirsPsr0 = array()

$fallbackDirsPsr4

private array<int, string> $fallbackDirsPsr4 = array()

$missingClasses

private array<string, bool> $missingClasses = array()

$prefixDirsPsr4

private array<string, array<int, string>> $prefixDirsPsr4 = array()

$prefixesPsr0

List of PSR-0 prefixes

private array<string, array<string, array<int, string>>> $prefixesPsr0 = array()

Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2')))

$prefixLengthsPsr4

private array<string, array<string, int>> $prefixLengthsPsr4 = array()

$registeredLoaders

private static array<string, 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<int, string>|string $paths[, bool $prepend = false ]) : void
Parameters
$prefix : string

The prefix

$paths : array<int, string>|string

The PSR-0 root directories

$prepend : bool = false

Whether to prepend the directories

Return values
void

addClassMap()

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

Class to filename map

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<int, string>|string $paths[, bool $prepend = false ]) : void
Parameters
$prefix : string

The prefix/namespace, with trailing '\'

$paths : array<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, string>
Return values
array<string, string>

Array of classname => path

getFallbackDirs()

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

getFallbackDirsPsr4()

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

getPrefixes()

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

getPrefixesPsr4()

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

getRegisteredLoaders()

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

public static getRegisteredLoaders() : array<string, self>
Return values
array<string, 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<int, string>|string $paths) : void
Parameters
$prefix : string

The prefix

$paths : array<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<int, string>|string $paths) : void
Parameters
$prefix : string

The prefix/namespace, with trailing '\'

$paths : array<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

initializeIncludeClosure()

private static initializeIncludeClosure() : void
Return values
void

Search results