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

__construct()

Creates a new ClassLoader instance.

bool
register()

Registers this instance as a class autoloader.

bool
unregister()

Unregisters this instance as a class autoloader.

bool
isRegistered()

Tells if this instance is currently registered as a class autoloader.

useIncludePath(bool $enabled = true)

Tells whether to use include_path as part of base paths.

setVerbose(bool $enabled)

Sets whether to return values and throw exceptions from loadClass.

setFileExtensions(array $extensions)

Sets list of dot included file extensions to use for finding files.

addBasePath(string|array $path, string|null $namespace = null)

Adds a PSR-0 compliant base path for searching classes.

array
getBasePaths()

Returns all known base paths.

addPrefixPath(string|array $path, string|null $namespace = null)

Adds a PSR-4 compliant prefix path for searching classes.

array
getPrefixPaths()

Returns all known prefix paths.

bool|null
loadClass(string $class)

Attempts to load the class using known class paths.

string|false
findFile(string $class)

Attempts to find a file for the given class using known paths.

bool
loadFile(string $file, string $class)

Includes the file and makes sure the class exists.

Details

__construct()

Creates a new ClassLoader instance.

bool register()

Registers this instance as a class autoloader.

Return Value

bool True if the registration was successful, false if not

bool unregister()

Unregisters this instance as a class autoloader.

Return Value

bool True if the unregistration was successful, false if not

bool isRegistered()

Tells if this instance is currently registered as a class autoloader.

Return Value

bool True if registered, false if not

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.

Parameters

bool $enabled True to use include_path, false to not use

Return Value

ClassLoader Returns self for call chaining

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.

Parameters

bool $enabled True to return values and exceptions, false to not

Return Value

ClassLoader Returns self for call chaining

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'.

Parameters

array $extensions Array of dot included file extensions to use

Return Value

ClassLoader Returns self for call chaining

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.

Parameters

string|array $path Single path, array of paths or an associative array
string|null $namespace Limit the path only to specific namespace

Return Value

ClassLoader Returns self for call chaining

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.

Return Value

array All known base paths

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.

Parameters

string|array $path Single path or array of paths
string|null $namespace The namespace prefix the given path replaces

Return Value

ClassLoader Returns self for call chaining

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).

Return Value

array All known prefix paths

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.

Parameters

string $class Full name of the class to load

Return Value

bool|null True if the class was loaded, false if not

Exceptions

RuntimeException If the class was not defined in the included file
InvalidArgumentException If the class already exists

string|false findFile(string $class)

Attempts to find a file for the given class using known paths.

Parameters

string $class Full name of the class

Return Value

string|false Path to the class file or false if not found

protected bool loadFile(string $file, string $class)

Includes the file and makes sure the class exists.

Parameters

string $file Full path to the file
string $class Full name of the class

Return Value

bool Always returns true

Exceptions

RuntimeException If the class was not defined in the included file