\Composer\AutoloadClassLoader

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.

Summary

Methods
Properties
Constants
__construct()
getPrefixes()
getPrefixesPsr4()
getFallbackDirs()
getFallbackDirsPsr4()
getClassMap()
addClassMap()
add()
addPsr4()
set()
setPsr4()
setUseIncludePath()
getUseIncludePath()
setClassMapAuthoritative()
isClassMapAuthoritative()
setApcuPrefix()
getApcuPrefix()
register()
unregister()
loadClass()
findFile()
getRegisteredLoaders()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
findFileWithExtension()
initializeIncludeClosure()
$includeFile
$vendorDir
$prefixLengthsPsr4
$prefixDirsPsr4
$fallbackDirsPsr4
$prefixesPsr0
$fallbackDirsPsr0
$useIncludePath
$classMap
$classMapAuthoritative
$missingClasses
$apcuPrefix
$registeredLoaders
N/A

Properties

$includeFile

$includeFile

$vendorDir

$vendorDir : ?string

Type

?string

$prefixLengthsPsr4

$prefixLengthsPsr4 : array[]

Type

array[]

$prefixDirsPsr4

$prefixDirsPsr4 : array[]

Type

array[]

$fallbackDirsPsr4

$fallbackDirsPsr4 : array[]

Type

array[]

$prefixesPsr0

$prefixesPsr0 : array[]

Type

array[]

$fallbackDirsPsr0

$fallbackDirsPsr0 : array[]

Type

array[]

$useIncludePath

$useIncludePath : bool

Type

bool

$classMap

$classMap : string[]

Type

string[]

$classMapAuthoritative

$classMapAuthoritative : bool

Type

bool

$missingClasses

$missingClasses : bool[]

Type

bool[]

$apcuPrefix

$apcuPrefix : ?string

Type

?string

$registeredLoaders

$registeredLoaders : self[]

Type

self[]

Methods

__construct()

__construct(?string  $vendorDir = null) : mixed

Parameters

?string $vendorDir

Returns

mixed —

getPrefixes()

getPrefixes() : string[]

Returns

string[] —

getPrefixesPsr4()

getPrefixesPsr4() : array[]

Returns

array[] —

getFallbackDirs()

getFallbackDirs() : array[]

Returns

array[] —

getFallbackDirsPsr4()

getFallbackDirsPsr4() : array[]

Returns

array[] —

getClassMap()

getClassMap() : string[]

Returns

string[] —

Array of classname => path

addClassMap()

addClassMap(string[]  $classMap) : void

Parameters

string[] $classMap

Class to filename map

add()

add(string  $prefix, string[]|string  $paths, bool  $prepend = false) : void

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

string[]|string $paths

The PSR-0 root directories

bool $prepend

Whether to prepend the directories

addPsr4()

addPsr4(string  $prefix, string[]|string  $paths, bool  $prepend = false) : void

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 '\'

string[]|string $paths

The PSR-4 base directories

bool $prepend

Whether to prepend the directories

Throws

\InvalidArgumentException

set()

set(string  $prefix, string[]|string  $paths) : void

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

Parameters

string $prefix

The prefix

string[]|string $paths

The PSR-0 base directories

setPsr4()

setPsr4(string  $prefix, string[]|string  $paths) : void

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 '\'

string[]|string $paths

The PSR-4 base directories

Throws

\InvalidArgumentException

setUseIncludePath()

setUseIncludePath(bool  $useIncludePath) : void

Turns on searching the include path for class files.

Parameters

bool $useIncludePath

getUseIncludePath()

getUseIncludePath() : bool

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

Returns

bool —

setClassMapAuthoritative()

setClassMapAuthoritative(bool  $classMapAuthoritative) : void

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

Parameters

bool $classMapAuthoritative

isClassMapAuthoritative()

isClassMapAuthoritative() : bool

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

Returns

bool —

setApcuPrefix()

setApcuPrefix(string|null  $apcuPrefix) : void

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

Parameters

string|null $apcuPrefix

getApcuPrefix()

getApcuPrefix() : string|null

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

Returns

string|null —

register()

register(bool  $prepend = false) : void

Registers this instance as an autoloader.

Parameters

bool $prepend

Whether to prepend the autoloader or not

unregister()

unregister() : void

Unregisters this instance as an autoloader.

loadClass()

loadClass(string  $class) : true|null

Loads the given class or interface.

Parameters

string $class

The name of the class

Returns

true|null —

True if loaded, null otherwise

findFile()

findFile(string  $class) : string|false

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

Parameters

string $class

The name of the class

Returns

string|false —

The path if found, false otherwise

getRegisteredLoaders()

getRegisteredLoaders() : self[]

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

Returns

self[] —

findFileWithExtension()

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

Parameters

string $class
string $ext

Returns

string|false —

initializeIncludeClosure()

initializeIncludeClosure() : void