Skip to content

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.

  • Full name: \Composer\Autoload\ClassLoader

See Also:

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

Properties

includeFile

private static callable $includeFile
  • This property is static.

vendorDir

private string|null $vendorDir

prefixLengthsPsr4

private array<string,array<string,int>> $prefixLengthsPsr4

prefixDirsPsr4

private array<string,list<string>> $prefixDirsPsr4

fallbackDirsPsr4

private list<string> $fallbackDirsPsr4

prefixesPsr0

List of PSR-0 prefixes

private array<string,array<string,list<string>>> $prefixesPsr0

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


fallbackDirsPsr0

private list<string> $fallbackDirsPsr0

useIncludePath

private bool $useIncludePath

classMap

private array<string,string> $classMap

classMapAuthoritative

private bool $classMapAuthoritative

missingClasses

private array<string,bool> $missingClasses

apcuPrefix

private string|null $apcuPrefix

registeredLoaders

private static array<string,self> $registeredLoaders
  • This property is static.

Methods

__construct

public __construct(string|null $vendorDir = null): mixed

Parameters:

Parameter Type Description
$vendorDir string|null

getPrefixes

public getPrefixes(): array<string,list<string>>

getPrefixesPsr4

public getPrefixesPsr4(): array<string,list<string>>

getFallbackDirs

public getFallbackDirs(): list<string>

getFallbackDirsPsr4

public getFallbackDirsPsr4(): list<string>

getClassMap

public getClassMap(): array<string,string>

Return Value:

Array of classname => path


addClassMap

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

Parameters:

Parameter Type Description
$classMap array Class to filename map

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, list<string>|string $paths, bool $prepend = false): void

Parameters:

Parameter Type Description
$prefix string The prefix
$paths list|string The PSR-0 root directories
$prepend bool Whether to prepend the directories

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, list<string>|string $paths, bool $prepend = false): void

Parameters:

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

Throws:


set

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

public set(string $prefix, list<string>|string $paths): void

Parameters:

Parameter Type Description
$prefix string The prefix
$paths list|string The PSR-0 base directories

setPsr4

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

public setPsr4(string $prefix, list<string>|string $paths): void

Parameters:

Parameter Type Description
$prefix string The prefix/namespace, with trailing '\'
$paths list|string The PSR-4 base directories

Throws:


setUseIncludePath

Turns on searching the include path for class files.

public setUseIncludePath(bool $useIncludePath): void

Parameters:

Parameter Type Description
$useIncludePath bool

getUseIncludePath

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

public getUseIncludePath(): bool

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:

Parameter Type Description
$classMapAuthoritative bool

isClassMapAuthoritative

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

public isClassMapAuthoritative(): bool

setApcuPrefix

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

public setApcuPrefix(string|null $apcuPrefix): void

Parameters:

Parameter Type Description
$apcuPrefix string|null

getApcuPrefix

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

public getApcuPrefix(): string|null

register

Registers this instance as an autoloader.

public register(bool $prepend = false): void

Parameters:

Parameter Type Description
$prepend bool Whether to prepend the autoloader or not

unregister

Unregisters this instance as an autoloader.

public unregister(): void

loadClass

Loads the given class or interface.

public loadClass(string $class): true|null

Parameters:

Parameter Type Description
$class string The name of the class

Return Value:

True if loaded, null otherwise


findFile

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

public findFile(string $class): string|false

Parameters:

Parameter Type Description
$class string The name of the class

Return Value:

The path if found, false otherwise


getRegisteredLoaders

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

public static getRegisteredLoaders(): array<string,self>
  • This method is static.

findFileWithExtension

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

Parameters:

Parameter Type Description
$class string
$ext string

initializeIncludeClosure

private static initializeIncludeClosure(): void
  • This method is static.


Automatically generated on 2025-03-18