Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

(no description)

File Size: 572 lines (16 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: ClassLoader  - X-Ref

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.

__construct($vendorDir = null)   X-Ref

param: ?string $vendorDir

getPrefixes()   X-Ref

return: string[]

getPrefixesPsr4()   X-Ref

return: array[]

getFallbackDirs()   X-Ref

return: array[]

getFallbackDirsPsr4()   X-Ref

return: array[]

getClassMap()   X-Ref

return: string[] Array of classname => path

addClassMap(array $classMap)   X-Ref

param: string[] $classMap Class to filename map
return: void

add($prefix, $paths, $prepend = false)   X-Ref
Registers a set of PSR-0 directories for a given prefix, either
appending or prepending to the ones previously set for this prefix.

param: string          $prefix  The prefix
param: string[]|string $paths   The PSR-0 root directories
param: bool            $prepend Whether to prepend the directories
return: void

addPsr4($prefix, $paths, $prepend = false)   X-Ref
Registers a set of PSR-4 directories for a given namespace, either
appending or prepending to the ones previously set for this namespace.

param: string          $prefix  The prefix/namespace, with trailing '\\'
param: string[]|string $paths   The PSR-4 base directories
param: bool            $prepend Whether to prepend the directories
return: void

set($prefix, $paths)   X-Ref
Registers a set of PSR-0 directories for a given prefix,
replacing any others previously set for this prefix.

param: string          $prefix The prefix
param: string[]|string $paths  The PSR-0 base directories
return: void

setPsr4($prefix, $paths)   X-Ref
Registers a set of PSR-4 directories for a given namespace,
replacing any others previously set for this namespace.

param: string          $prefix The prefix/namespace, with trailing '\\'
param: string[]|string $paths  The PSR-4 base directories
return: void

setUseIncludePath($useIncludePath)   X-Ref
Turns on searching the include path for class files.

param: bool $useIncludePath
return: void

getUseIncludePath()   X-Ref
Can be used to check if the autoloader uses the include path to check
for classes.

return: bool

setClassMapAuthoritative($classMapAuthoritative)   X-Ref
Turns off searching the prefix and fallback directories for classes
that have not been registered with the class map.

param: bool $classMapAuthoritative
return: void

isClassMapAuthoritative()   X-Ref
Should class lookup fail if not found in the current class map?

return: bool

setApcuPrefix($apcuPrefix)   X-Ref
APCu prefix to use to cache found/not-found classes, if the extension is enabled.

param: string|null $apcuPrefix
return: void

getApcuPrefix()   X-Ref
The APCu prefix in use, or null if APCu caching is not enabled.

return: string|null

register($prepend = false)   X-Ref
Registers this instance as an autoloader.

param: bool $prepend Whether to prepend the autoloader or not
return: void

unregister()   X-Ref
Unregisters this instance as an autoloader.

return: void

loadClass($class)   X-Ref
Loads the given class or interface.

param: string    $class The name of the class
return: true|null True if loaded, null otherwise

findFile($class)   X-Ref
Finds the path to the file where the class is defined.

param: string $class The name of the class
return: string|false The path if found, false otherwise

getRegisteredLoaders()   X-Ref
Returns the currently registered loaders indexed by their corresponding vendor directories.

return: self[]

findFileWithExtension($class, $ext)   X-Ref

param: string       $class
param: string       $ext
return: string|false

includeFile($file)   X-Ref
Scope isolated include.

Prevents access to $this/self from included files.

param: string $file
return: void