PHP Class Contao\ClassLoader

The class stores namespaces and classes and automatically loads the class files upon their first usage. It uses a mapper array to support complex nesting and arbitrary subfolders to store the class files in. Usage: ClassLoader::addNamespace('Custom'); ClassLoader::addClass('Custom\\Calendar', 'calendar/Calendar.php');
Deprecation: Deprecated since Contao 4.2, to be removed in Contao 5. Use the Composer autoloader instead.
Show file Open project: contao/core-bundle Class Usage Examples

Protected Properties

Property Type Description
$classes array Known classes
$namespaces array Known namespaces

Public Methods

Method Description
addClass ( string $class, string $file ) Add a new class with its file path
addClasses ( array $classes ) Add multiple new classes with their file paths
addNamespace ( string $name ) Add a new namespace
addNamespaces ( array $names ) Add multiple new namespaces
getClasses ( ) : array Return the classes as array.
getNamespaces ( ) : array Return the namespaces as array
load ( string $class ) Autoload a class and create an alias in the global namespace
register ( ) Register the autoloader
scanAndRegister ( ) Scan the module directories for config/autoload.php files and then register the autoloader on the SPL stack

Protected Methods

Method Description
findClass ( string $class ) : string | null Search the namespaces for a matching entry

Method Details

addClass() public static method

Add a new class with its file path
public static addClass ( string $class, string $file )
$class string The class name
$file string The path to the class file

addClasses() public static method

Add multiple new classes with their file paths
public static addClasses ( array $classes )
$classes array An array of classes

addNamespace() public static method

Add a new namespace
public static addNamespace ( string $name )
$name string The namespace name

addNamespaces() public static method

Add multiple new namespaces
public static addNamespaces ( array $names )
$names array An array of namespace names

findClass() protected static method

Search the namespaces for a matching entry
protected static findClass ( string $class ) : string | null
$class string The class name
return string | null The full path including the namespace or null

getClasses() public static method

Return the classes as array.
public static getClasses ( ) : array
return array An array of all classes

getNamespaces() public static method

Return the namespaces as array
public static getNamespaces ( ) : array
return array An array of all namespaces

load() public static method

To preserve backwards compatibility with Contao 2 extensions, all core classes will be aliased into the global namespace.
public static load ( string $class )
$class string The class name

register() public static method

Register the autoloader
public static register ( )

scanAndRegister() public static method

Scan the module directories for config/autoload.php files and then register the autoloader on the SPL stack
public static scanAndRegister ( )

Property Details

$classes protected static property

Known classes
protected static array $classes
return array

$namespaces protected static property

Known namespaces
protected static array $namespaces
return array