ClassLoader
class ClassLoader (View source)
Class loader that supports both PSR-0 and PSR-4 autoloading standards.
The purpose autoloading classes is to load the class files only as they are needed. This reduces the overall page overhead, as every file does not need to be requested on every request. It also makes managing class loading much simpler.
The standard practice in autoloading is to place classes in files that are named according to the class names and placed in a directory hierarchy according to their namespace. ClassLoader supports two such standard autoloading practices: PSR-0 and PSR-4.
Class paths can be provided as base paths, which are appended with the full class name (as per PSR-0), or as prefix paths that can replace part of the namespace with a specific directory (as per PSR-4). Depending on which kind of paths are added, the underscores may or may not be treated as namespace separators.
Properties
protected bool | $verbose |
Methods
Creates a new ClassLoader instance.
Registers this instance as a class autoloader.
Unregisters this instance as a class autoloader.
Tells if this instance is currently registered as a class autoloader.
Tells whether to use include_path as part of base paths.
Sets whether to return values and throw exceptions from loadClass.
Sets list of dot included file extensions to use for finding files.
Adds a PSR-0 compliant base path for searching classes.
Returns all known base paths.
Adds a PSR-4 compliant prefix path for searching classes.
Returns all known prefix paths.
Attempts to load the class using known class paths.
Attempts to find a file for the given class using known paths.
Includes the file and makes sure the class exists.
Details
at line 53
__construct()
Creates a new ClassLoader instance.
at line 67
bool
register()
Registers this instance as a class autoloader.
at line 76
bool
unregister()
Unregisters this instance as a class autoloader.
at line 85
bool
isRegistered()
Tells if this instance is currently registered as a class autoloader.
at line 100
ClassLoader
useIncludePath(bool $enabled = true)
Tells whether to use include_path as part of base paths.
When enabled, the directory paths in include_path are treated as base paths where to look for classes. This option defaults to false for PSR-4 compliance.
at line 117
ClassLoader
setVerbose(bool $enabled)
Sets whether to return values and throw exceptions from loadClass.
PSR-4 requires that autoloaders do not return values and do not throw exceptions from the autoloader. By default, the verbose mode is set to false for PSR-4 compliance.
at line 133
ClassLoader
setFileExtensions(array $extensions)
Sets list of dot included file extensions to use for finding files.
If no list of extensions is provided, the extension array defaults to just '.php'.
at line 173
ClassLoader
addBasePath(string|array $path, string|null $namespace = null)
Adds a PSR-0 compliant base path for searching classes.
In PSR-0, the class namespace structure directly reflects the location in the directory tree. A base path indicates the base directory where to search for classes. For example, if the class 'Foo\Bar', is defined in '/usr/lib/Foo/Bar.php', you would simply need to add the directory '/usr/lib' by calling:
addBasePath('/usr/lib')
Additionally, you may specify that the base path applies only to a specific namespace. You can do this by adding the namespace as the second parameter. For example, if you would like the path in the previous example to only apply to the namespace 'Foo', you could do so by calling:
addBasePath('/usr/lib/', 'Foo')
Note that as per PSR-0, the underscores in the class name are treated as namespace separators. Therefore 'Foo_Bar_Baz', would need to reside in 'Foo/Bar/Baz.php'. Regardless of whether the namespace is indicated by namespace separators or underscores, the namespace parameter must be defined using namespace separators, e.g 'Foo\Bar'.
In addition to providing a single path as a string, you may also provide an array of paths. It is also possible to provide an associative array where the keys indicate the namespaces. Each value in the associative array may also be a string or an array of paths.
at line 192
array
getBasePaths()
Returns all known base paths.
The paths will be returned as an associative array. The key indicates the namespace and the values are arrays that contain all paths that apply to that specific namespace. Paths that apply to all namespaces can be found inside the key '' (i.e. empty string). Note that the array does not include the paths in include_path even if the use of include_path is enabled.
at line 220
ClassLoader
addPrefixPath(string|array $path, string|null $namespace = null)
Adds a PSR-4 compliant prefix path for searching classes.
In PSR-4, it is possible to replace part of namespace with specific path in the directory tree instead of requiring the entire namespace structure to be present in the directory tree. For example, if the class 'Vendor\Library\Class' is located in '/usr/lib/Library/src/Class.php', You would need to add the path '/usr/lib/Library/src' to the namespace 'Vendor\Library' by calling:
addPrefixPath('/usr/lib/Library/src', 'Vendor\Library')
If the method is called without providing a namespace, then the paths work similarly to paths added via addBasePath(), except that the underscores in the file name are not treated as namespace separators.
Similarly to addBasePath(), the paths may be provided as an array or you can just provide a single associative array as the parameter.
at line 237
array
getPrefixPaths()
Returns all known prefix paths.
The paths will be returned as an associative array. The key indicates the namespace and the values are arrays that contain all paths that apply to that specific namespace. Paths that apply to all namespaces can be found inside the key '' (i.e. empty string).
at line 299
bool|null
loadClass(string $class)
Attempts to load the class using known class paths.
The classes will be searched using the prefix paths, base paths and the include_path (if enabled) in that order. Other than that, the autoloader makes no guarantees about the order of the searched paths.
If verbose mode is enabled, then the method will return true if the class loading was successful and false if not. Additionally the method will throw an exception if the class already exists or if the class was not defined in the file that was included.
at line 339
string|false
findFile(string $class)
Attempts to find a file for the given class using known paths.
at line 351
protected bool
loadFile(string $file, string $class)
Includes the file and makes sure the class exists.