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
Table of Contents
Properties
- $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>>
- $includeFile : callable(string): void
- $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
Methods
- __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
- initializeIncludeClosure() : void
Properties
$apcuPrefix
private
string|null
$apcuPrefix
$classMap
private
array<string|int, string>
$classMap
= array()
Tags
$classMapAuthoritative
private
bool
$classMapAuthoritative
= false
$fallbackDirsPsr0
private
array<string|int, array<string|int, mixed>>
$fallbackDirsPsr0
= array()
Tags
$fallbackDirsPsr4
private
array<string|int, array<string|int, mixed>>
$fallbackDirsPsr4
= array()
Tags
$includeFile
private
static callable(string): void
$includeFile
$missingClasses
private
array<string|int, bool>
$missingClasses
= array()
Tags
$prefixDirsPsr4
private
array<string|int, array<string|int, mixed>>
$prefixDirsPsr4
= array()
Tags
$prefixesPsr0
private
array<string|int, array<string|int, mixed>>
$prefixesPsr0
= array()
Tags
$prefixLengthsPsr4
private
array<string|int, array<string|int, mixed>>
$prefixLengthsPsr4
= array()
Tags
$registeredLoaders
private
static array<string|int, self>
$registeredLoaders
= array()
$useIncludePath
private
bool
$useIncludePath
= false
$vendorDir
private
string|null
$vendorDir
Methods
__construct()
public
__construct([string|null $vendorDir = null ]) : mixed
Parameters
- $vendorDir : string|null = null
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
addClassMap()
public
addClassMap(array<string|int, string> $classMap) : void
Parameters
- $classMap : array<string|int, string>
-
Class to filename map
Tags
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
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|nullgetClassMap()
public
getClassMap() : array<string|int, string>
Tags
Return values
array<string|int, string> —Array of classname => path
getFallbackDirs()
public
getFallbackDirs() : array<string|int, array<string|int, mixed>>
Tags
Return values
array<string|int, array<string|int, mixed>>getFallbackDirsPsr4()
public
getFallbackDirsPsr4() : array<string|int, array<string|int, mixed>>
Tags
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
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
boolisClassMapAuthoritative()
Should class lookup fail if not found in the current class map?
public
isClassMapAuthoritative() : bool
Return values
boolloadClass()
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
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
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
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
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
setUseIncludePath()
Turns on searching the include path for class files.
public
setUseIncludePath(bool $useIncludePath) : void
Parameters
- $useIncludePath : bool
unregister()
Unregisters this instance as an autoloader.
public
unregister() : void
findFileWithExtension()
private
findFileWithExtension(string $class, string $ext) : string|false
Parameters
- $class : string
- $ext : string
Return values
string|falseinitializeIncludeClosure()
private
static initializeIncludeClosure() : void