ClassLoader
in package
A <tt>ClassLoader</tt> 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 , a ClassLoader
relies on the PHP include_path
.
Tags
Table of Contents
Properties
- $fileExtension : string
- PHP file extension.
- $includePath : string|null
- Current include path.
- $namespace : string|null
- Current namespace.
- $namespaceSeparator : string
- PHP namespace separator.
Methods
- __construct() : mixed
- Creates a new <tt>ClassLoader</tt> that loads classes of the specified namespace from the specified include path.
- canLoadClass() : bool
- Asks this ClassLoader whether it can potentially load the class (file) with the given name.
- classExists() : bool
- 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.
- getClassLoader() : ClassLoader|null
- Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible for (and is able to load) the class with the given name.
- getFileExtension() : string
- Gets the file extension of class files in the namespace of this ClassLoader.
- getIncludePath() : string|null
- Gets the base include path for all class files in the namespace of this ClassLoader.
- getNamespaceSeparator() : string
- Gets the namespace separator used by classes in the namespace of this ClassLoader.
- loadClass() : bool
- Loads the given class or interface.
- register() : void
- Registers this ClassLoader on the SPL autoload stack.
- setFileExtension() : void
- Sets the file extension of class files in the namespace of this ClassLoader.
- setIncludePath() : void
- Sets the base include path for all class files in the namespace of this ClassLoader.
- setNamespaceSeparator() : void
- Sets the namespace separator used by classes in the namespace of this ClassLoader.
- unregister() : void
- Removes this ClassLoader from the SPL autoload stack.
- typeExists() : bool
- Checks whether a given type exists
Properties
$fileExtension
PHP file extension.
protected
string
$fileExtension
= '.php'
$includePath
Current include path.
protected
string|null
$includePath
$namespace
Current namespace.
protected
string|null
$namespace
$namespaceSeparator
PHP namespace separator.
protected
string
$namespaceSeparator
= '\\'
Methods
__construct()
Creates a new <tt>ClassLoader</tt> that loads classes of the specified namespace from the specified include path.
public
__construct([string|null $ns = null ][, string|null $includePath = null ]) : mixed
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
- $ns : string|null = null
-
The namespace of the classes to load.
- $includePath : string|null = null
-
The base include path to use.
canLoadClass()
Asks this ClassLoader whether it can potentially load the class (file) with the given name.
public
canLoadClass(string $className) : bool
Parameters
- $className : string
-
The fully-qualified name of the class.
Tags
Return values
bool —TRUE if this ClassLoader can load the class, FALSE otherwise.
classExists()
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.
public
static classExists(string $className) : bool
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, 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
- $className : string
-
The fully-qualified name of the class.
Tags
Return values
bool —TRUE if the class exists as per the definition given above, FALSE otherwise.
getClassLoader()
Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible for (and is able to load) the class with the given name.
public
static getClassLoader(string $className) : ClassLoader|null
Parameters
- $className : string
-
The name of the class.
Tags
Return values
ClassLoader|null —The ClassLoader for the class or NULL if no such ClassLoader exists.
getFileExtension()
Gets the file extension of class files in the namespace of this ClassLoader.
public
getFileExtension() : string
Return values
stringgetIncludePath()
Gets the base include path for all class files in the namespace of this ClassLoader.
public
getIncludePath() : string|null
Return values
string|nullgetNamespaceSeparator()
Gets the namespace separator used by classes in the namespace of this ClassLoader.
public
getNamespaceSeparator() : string
Return values
stringloadClass()
Loads the given class or interface.
public
loadClass(string $className) : bool
Parameters
- $className : string
-
The name of the class to load.
Tags
Return values
bool —TRUE if the class has been successfully loaded, FALSE otherwise.
register()
Registers this ClassLoader on the SPL autoload stack.
public
register() : void
setFileExtension()
Sets the file extension of class files in the namespace of this ClassLoader.
public
setFileExtension(string $fileExtension) : void
Parameters
- $fileExtension : string
setIncludePath()
Sets the base include path for all class files in the namespace of this ClassLoader.
public
setIncludePath(string|null $includePath) : void
Parameters
- $includePath : string|null
setNamespaceSeparator()
Sets the namespace separator used by classes in the namespace of this ClassLoader.
public
setNamespaceSeparator(string $sep) : void
Parameters
- $sep : string
-
The separator to use.
unregister()
Removes this ClassLoader from the SPL autoload stack.
public
unregister() : void
typeExists()
Checks whether a given type exists
private
static typeExists(string $type[, bool $autoload = false ]) : bool
Parameters
- $type : string
- $autoload : bool = false