Loader
Loader
Application path resolver and autoloader coordinator.
class LoaderMaintains categorized search paths for project resources, resolves files by path type, and provides an SPL-compatible class autoloader for application namespaces.
This class is managed as a singleton and should be accessed through Loader::getInstance() or initialized once via Loader::createInstance().
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
Absolute application root path used to resolve relative search paths.
private string $appPathinstance
Singleton loader instance.
private self $instanceconfigPath
private string $configPath = 'configs'Methods
__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 |
findAppPath
Attempts to locate the application path by searching for an 'app' directory
public findAppPath(?string $searchPath): ?stringcontaining a 'Configs' subdirectory, starting from the provided search path or the current working directory. The search ascends up to 16 directory levels. If the 'APPLICATION_PATH' environment variable is set, its value is used.
Parameters
| Parameter | Type | Description |
|---|---|---|
$searchPath | string | Optional starting directory for the search. Defaults to current working directory. |
Generated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:23 +0000