Loader
Loader
Global class file loader.
This class contains methods for auto-loading classes from files in the Hazaar library path. Ordinarily there will be no need for developers to use this class directly but it does contain a few methods for working with application paths and library files.
This class is not meant to be instantiated directly and instances should be retrieved using the Loader::getInstance() method. Don't worry about the instance being created, it is automatically created when the application starts.
Example
$loader = Hazaar\Loader::getInstance();!!! notice The loader class is loaded automatically when starting the application. There should be no need to use the Loader instance directly and static methods have been provided for some extra functionality.
!!! warning Instantiating this class directly can have undefined results.
Properties
paths
public array $pathsappPath
private string $appPathinstance
private self $instanceMethods
__construct
Initialise a new loader.
protected __construct(string $appPath): voidWarning
Do NOT instantiate this class directly. See Loader::getInstance() on how to get a new Loader instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
$appPath | string |
getInstance
Retrieves the singleton instance of the Loader class.
public getInstance(): ?LoadercreateInstance
Creates a new instance of the Loader class with the specified path.
public createInstance(string $path): LoaderThis method will throw a RuntimeException if an instance of Loader already exists. Use Loader::getInstance() to retrieve the existing instance instead.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the path to initialize the Loader with |
register
Registers the autoloader function with SPL.
public register(): voidThis method adds the 'loadClassFromFile' method of this instance to the SPL autoload stack, allowing automatic loading of classes.
unregister
Unregisters the autoloader by removing the loadClassFromFile method from the SPL autoload stack.
public unregister(): voidThis method disables the autoloading functionality previously registered by this loader instance.
addIncludePath
Adds a new path to the PHP include_path configuration.
public addIncludePath(string $path): voidThis method appends the specified path to the current include_path, allowing PHP to search for files in the added directory when including or requiring files.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the directory path to add to the include_path |
addSearchPath
Add a new search path for loading classes from library files.
public addSearchPath(FilePath $type, string $path): voidThe path type can be anything if you are using the loader to load your own library files. There are built in path types for loading Hazaar library files.
- FilePath::ROOT - Path that contains the whole project
- FilePath::MODEL - Path contains model classes
- FilePath::VIEW - Path contains view files.
- FilePath::CONTROLLER - Path contains controller classes.
- FilePath
runDirect()method. - FilePath::CONFIG - Configuration files
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | FilePath | the path type to add |
$path | string | the path to add |
setSearchPath
Sets the search path for a file type.
public setSearchPath(mixed $type, mixed $path): voidThis is the same as addSearchPath except that it overwrites any existing paths.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | mixed | the path type to add |
$path | mixed | the path to add |
addSearchPaths
Add multiple search paths from an array.
public addSearchPaths(array $array): voidParameters
| Parameter | Type | Description |
|---|---|---|
$array | array | Array containing type/path pairs |
getSearchPaths
Return an array of search paths for this loader instance.
public getSearchPaths(?FilePath $type): voidParameters
| Parameter | Type | Description |
|---|---|---|
$type | FilePath |
isAbsolutePath
Checks if a given path is an absolute path.
public isAbsolutePath(string $path): boolParameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the path to check |
getFilePath
Return the absolute filesystem path to a file.
public getFilePath(FilePath $type, ?string $searchFile): ?stringBy default this method uses the application path as the base path.
This method also checks that the file exists. If the file does not exist then null will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | FilePath | The path type to search. See Loader::addSearchPath() |
$searchFile | string | The file to search for |
searchFile
Searches for a file within the specified loader paths.
public searchFile(FilePath $type, ?string $searchFile): ?stringParameters
| Parameter | Type | Description |
|---|---|---|
$type | FilePath | the type of file path to search within |
$searchFile | string | The name of the file to search for. If null, returns the first path. |
resolve
Resolve a filename within any of the search paths.
public resolve(string $filename): ?stringParameters
| Parameter | Type | Description |
|---|---|---|
$filename | string |
loadClassFromFile
Loads a class file based on the provided class name.
public loadClassFromFile(string $className): voidThis method splits the class name into parts using non-word characters and underscores as delimiters. It then checks if the first part of the class name is 'App', which acts as a namespace key to restrict the loadable path to that of the application itself. If the prefix is 'App', it constructs the file path from the class name parts and attempts to load the file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$className | string | the fully qualified name of the class to load |
Generated by Hazaar API Doc Generator on Sun, 01 Mar 2026 09:22:25 +0000