class ClassLoader (View source)

deprecated the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.

A ClassLoader is an autoloader for class files that can be installed on the SPL autoload stack. It is a class loader that either loads only classes of a specific namespace or all namespaces and it is suitable for working together with other autoloaders in the SPL autoload stack.

If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader relies on the PHP include_path.

Properties

protected string $fileExtension PHP file extension.
protected string|null $namespace Current namespace.
protected string|null $includePath Current include path.
protected string $namespaceSeparator PHP namespace separator.

Methods

__construct(string|null $ns = null, string|null $includePath = null)

Creates a new ClassLoader that loads classes of the specified namespace from the specified include path.

void
setNamespaceSeparator(string $sep)

Sets the namespace separator used by classes in the namespace of this ClassLoader.

string
getNamespaceSeparator()

Gets the namespace separator used by classes in the namespace of this ClassLoader.

void
setIncludePath(string|null $includePath)

Sets the base include path for all class files in the namespace of this ClassLoader.

string|null
getIncludePath()

Gets the base include path for all class files in the namespace of this ClassLoader.

void
setFileExtension(string $fileExtension)

Sets the file extension of class files in the namespace of this ClassLoader.

string
getFileExtension()

Gets the file extension of class files in the namespace of this ClassLoader.

void
register()

Registers this ClassLoader on the SPL autoload stack.

void
unregister()

Removes this ClassLoader from the SPL autoload stack.

boolean
loadClass(string $className)

Loads the given class or interface.

boolean
canLoadClass(string $className)

Asks this ClassLoader whether it can potentially load the class (file) with the given name.

static boolean
classExists(string $className)

Checks whether a class with a given name exists. A class "exists" if it is either already defined in the current request or if there is an autoloader on the SPL autoload stack that is a) responsible for the class in question and b) is able to load a class file in which the class definition resides.

static ClassLoader|null
getClassLoader(string $className)

Gets the ClassLoader from the SPL autoload stack that is responsible for (and is able to load) the class with the given name.

Details

__construct(string|null $ns = null, string|null $includePath = null)

Creates a new ClassLoader that loads classes of the specified namespace from the specified include path.

If no include path is given, the ClassLoader relies on the PHP include_path. If neither a namespace nor an include path is given, the ClassLoader will be responsible for loading all classes, thereby relying on the PHP include_path.

Parameters

string|null $ns The namespace of the classes to load.
string|null $includePath The base include path to use.

void setNamespaceSeparator(string $sep)

Sets the namespace separator used by classes in the namespace of this ClassLoader.

Parameters

string $sep The separator to use.

Return Value

void

string getNamespaceSeparator()

Gets the namespace separator used by classes in the namespace of this ClassLoader.

Return Value

string

void setIncludePath(string|null $includePath)

Sets the base include path for all class files in the namespace of this ClassLoader.

Parameters

string|null $includePath

Return Value

void

string|null getIncludePath()

Gets the base include path for all class files in the namespace of this ClassLoader.

Return Value

string|null

void setFileExtension(string $fileExtension)

Sets the file extension of class files in the namespace of this ClassLoader.

Parameters

string $fileExtension

Return Value

void

string getFileExtension()

Gets the file extension of class files in the namespace of this ClassLoader.

Return Value

string

void register()

Registers this ClassLoader on the SPL autoload stack.

Return Value

void

void unregister()

Removes this ClassLoader from the SPL autoload stack.

Return Value

void

boolean loadClass(string $className)

Loads the given class or interface.

Parameters

string $className The name of the class to load.

Return Value

boolean TRUE if the class has been successfully loaded, FALSE otherwise.

boolean canLoadClass(string $className)

Asks this ClassLoader whether it can potentially load the class (file) with the given name.

Parameters

string $className The fully-qualified name of the class.

Return Value

boolean TRUE if this ClassLoader can load the class, FALSE otherwise.

static boolean classExists(string $className)

Checks whether a class with a given name exists. A class "exists" if it is either already defined in the current request or if there is an autoloader on the SPL autoload stack that is a) responsible for the class in question and b) is able to load a class file in which the class definition resides.

If the class is not already defined, each autoloader in the SPL autoload stack is asked whether it is able to tell if the class exists. If the autoloader is a ClassLoader, {@link canLoadClass} is used, otherwise the autoload function of the autoloader is invoked and expected to return a value that evaluates to TRUE if the class (file) exists. As soon as one autoloader reports that the class exists, TRUE is returned.

Note that, depending on what kinds of autoloaders are installed on the SPL autoload stack, the class (file) might already be loaded as a result of checking for its existence. This is not the case with a ClassLoader, who separates these responsibilities.

Parameters

string $className The fully-qualified name of the class.

Return Value

boolean TRUE if the class exists as per the definition given above, FALSE otherwise.

static ClassLoader|null getClassLoader(string $className)

Gets the ClassLoader from the SPL autoload stack that is responsible for (and is able to load) the class with the given name.

Parameters

string $className The name of the class.

Return Value

ClassLoader|null The ClassLoader for the class or NULL if no such ClassLoader exists.