Class Composer\Autoload\ClassLoader

InheritanceComposer\Autoload\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.

See also:

Public Methods

Hide inherited methods

MethodDescriptionDefined By
add() Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix. Composer\Autoload\ClassLoader
addClassMap() Composer\Autoload\ClassLoader
addPsr4() Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace. Composer\Autoload\ClassLoader
findFile() Finds the path to the file where the class is defined. Composer\Autoload\ClassLoader
getClassMap() Composer\Autoload\ClassLoader
getFallbackDirs() Composer\Autoload\ClassLoader
getFallbackDirsPsr4() Composer\Autoload\ClassLoader
getPrefixes() Composer\Autoload\ClassLoader
getPrefixesPsr4() Composer\Autoload\ClassLoader
getUseIncludePath() Can be used to check if the autoloader uses the include path to check for classes. Composer\Autoload\ClassLoader
isClassMapAuthoritative() Should class lookup fail if not found in the current class map? Composer\Autoload\ClassLoader
loadClass() Loads the given class or interface. Composer\Autoload\ClassLoader
register() Registers this instance as an autoloader. Composer\Autoload\ClassLoader
set() Registers a set of PSR-0 directories for a given prefix, replacing any others previously set for this prefix. Composer\Autoload\ClassLoader
setClassMapAuthoritative() Turns off searching the prefix and fallback directories for classes that have not been registered with the class map. Composer\Autoload\ClassLoader
setPsr4() Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for this namespace. Composer\Autoload\ClassLoader
setUseIncludePath() Turns on searching the include path for class files. Composer\Autoload\ClassLoader
unregister() Unregisters this instance as an autoloader. Composer\Autoload\ClassLoader

Method Details

add() public method

Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix.

public void add ( $prefix, $paths, $prepend false )
$prefix string

The prefix

$paths array|string

The PSR-0 root directories

$prepend bool

Whether to prepend the directories

addClassMap() public method

public void addClassMap ( array $classMap )
$classMap array

Class to filename map

addPsr4() public method

Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace.

public void addPsr4 ( $prefix, $paths, $prepend false )
$prefix string

The prefix/namespace, with trailing '\'

$paths array|string

The PSR-4 base directories

$prepend bool

Whether to prepend the directories

throws InvalidArgumentException
findFile() public method

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

public string|false findFile ( $class )
$class string

The name of the class

return string|false

The path if found, false otherwise

getClassMap() public method

public void getClassMap ( )
getFallbackDirs() public method

public void getFallbackDirs ( )
getFallbackDirsPsr4() public method

public void getFallbackDirsPsr4 ( )
getPrefixes() public method

public void getPrefixes ( )
getPrefixesPsr4() public method

public void getPrefixesPsr4 ( )
getUseIncludePath() public method

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

public bool getUseIncludePath ( )
isClassMapAuthoritative() public method

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

public bool isClassMapAuthoritative ( )
loadClass() public method

Loads the given class or interface.

public bool|null loadClass ( $class )
$class string

The name of the class

return bool|null

True if loaded, null otherwise

register() public method

Registers this instance as an autoloader.

public void register ( $prepend false )
$prepend bool

Whether to prepend the autoloader or not

set() public method

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

public void set ( $prefix, $paths )
$prefix string

The prefix

$paths array|string

The PSR-0 base directories

setClassMapAuthoritative() public method

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

public void setClassMapAuthoritative ( $classMapAuthoritative )
$classMapAuthoritative bool
setPsr4() public method

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

public void setPsr4 ( $prefix, $paths )
$prefix string

The prefix/namespace, with trailing '\'

$paths array|string

The PSR-4 base directories

throws InvalidArgumentException
setUseIncludePath() public method

Turns on searching the include path for class files.

public void setUseIncludePath ( $useIncludePath )
$useIncludePath bool
unregister() public method

Unregisters this instance as an autoloader.

public void unregister ( )