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
- This property is static.
vendorDir
prefixLengthsPsr4
prefixDirsPsr4
fallbackDirsPsr4
prefixesPsr0
List of PSR-0 prefixes
Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2')))
fallbackDirsPsr0
useIncludePath
classMap
classMapAuthoritative
missingClasses
apcuPrefix
registeredLoaders
- This property is static.
Methods
__construct
Parameters:
Parameter | Type | Description |
---|---|---|
$vendorDir |
string|null |
getPrefixes
getPrefixesPsr4
getFallbackDirs
getFallbackDirsPsr4
getClassMap
Return Value:
Array of classname => path
addClassMap
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.
Parameters:
Parameter | Type | Description |
---|---|---|
$prefix |
string | The prefix |
$paths |
list |
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.
Parameters:
Parameter | Type | Description |
---|---|---|
$prefix |
string | The prefix/namespace, with trailing '\' |
$paths |
list |
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.
Parameters:
Parameter | Type | Description |
---|---|---|
$prefix |
string | The prefix |
$paths |
list |
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.
Parameters:
Parameter | Type | Description |
---|---|---|
$prefix |
string | The prefix/namespace, with trailing '\' |
$paths |
list |
The PSR-4 base directories |
Throws:
setUseIncludePath
Turns on searching the include path for class files.
Parameters:
Parameter | Type | Description |
---|---|---|
$useIncludePath |
bool |
getUseIncludePath
Can be used to check if the autoloader uses the include path to check for classes.
setClassMapAuthoritative
Turns off searching the prefix and fallback directories for classes that have not been registered with the class map.
Parameters:
Parameter | Type | Description |
---|---|---|
$classMapAuthoritative |
bool |
isClassMapAuthoritative
Should class lookup fail if not found in the current class map?
setApcuPrefix
APCu prefix to use to cache found/not-found classes, if the extension is enabled.
Parameters:
Parameter | Type | Description |
---|---|---|
$apcuPrefix |
string|null |
getApcuPrefix
The APCu prefix in use, or null if APCu caching is not enabled.
register
Registers this instance as an autoloader.
Parameters:
Parameter | Type | Description |
---|---|---|
$prepend |
bool | Whether to prepend the autoloader or not |
unregister
Unregisters this instance as an autoloader.
loadClass
Loads the given class or interface.
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.
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.
- This method is static.
findFileWithExtension
Parameters:
Parameter | Type | Description |
---|---|---|
$class |
string | |
$ext |
string |
initializeIncludeClosure
- This method is static.
Automatically generated on 2025-03-18