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 $pathsapplicationPath
private string $applicationPathinstance
private self $instanceMethods
__construct
Initialise a new loader.
protected __construct(string $applicationPath): void!!! warning Do NOT instantiate this class directly. See Loader::getInstance() on how to get a new Loader instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
$applicationPath | string |
getInstance
Return the current instance of the Loader object.
public getInstance(): ?LoadercreateInstance
public createInstance(string $path): LoaderParameters
| Parameter | Type | Description |
|---|---|---|
$path | string |
register
Register this loader instance as a class autoloader.
public register(): voidunregister
Unregister this loader instance as a class autoloader.
public unregister(): voidaddIncludePath
public addIncludePath(string $path): voidParameters
| Parameter | Type | Description |
|---|---|---|
$path | string |
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
public searchFile(FilePath $type, ?string $searchFile): ?stringParameters
| Parameter | Type | Description |
|---|---|---|
$type | FilePath | |
$searchFile | string |
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 Mon, 27 Oct 2025 13:01:29 +0000