This is a module of Autoloader that works like this:

  • Determines whether the class name is relevant for the given loader. If not, return false and Autoloader will give the next registered loader an opportunity.
  • If it is relevant for the class name, it builds a path to the script that contains the class. It should do this as quickly as possible (with as little overhead as possible). It then returns the found path as the return value. The main Autoloader system class takes care of the rest of the class loading process. This is no longer a matter for the individual Loader - its job is just to return the path to the class script.

Each loader must inherit from the Jet\Autoloader_Loader class.

Example of loader (Jet library loader - the framework itself, i.e. script ~/library/Jet/JetAutoloader.php):

use Jet\Autoloader_Loader;

return new class extends 
    public function 
getAutoloaderName() : string
    public function 
getScriptPathstring $class_name ): bool|string
str_starts_with($class_name'Jet\\')) {
SysConf_Path::getLibrary() . $this->classNameToPath$class_name );

Two basic types of autoloaders

There are two basic second autoloaders:

  • Library Autoloaders
    Each library (in the ~/library/ directory) can have its own autoloader in the JetAutoloader.php script - as shown in the example above. So any library can be easily added with its own loader, which will perfectly cooperate with the system.

    (Paths and names can of course be changed by system configuration.)

  • Application Autoloaders
    In the ~/application/Autoloaders/ directory (here you can also change everything using system configuration) you will find two basic autoloaders for loading your application classes. There is an autoloader of general application classes (ApplicationClasses.php) and application modules classes (ApplicationModules.php).

    Of course, there is nothing stopping you from creating another autoloader exactly according to your needs.

For all these basic autoloaders, there is always one loader in each script, which is returned as an instance of an anonymous class.

Creating and registering (any) autoloader

While the general autoloader system provides methods for loading library autoloaders and application class autoloaders, it is true that any autoloader can be easily created and registered.

The only condition is that the autoloader must inherit from the abstract class Jet\Autoloader_Loader and implement the methods getAutoloaderName (defines the name of the autoloader used, for example, for generating error messages) and getScriptPath (generates the name of the script of the class - if the autoloader is relevant for the given situation).

In general, you can register an autoloader as follows:

Autoloader::register( new class extends Autoloader_Loader {
    public const 
'MyNamespace\\Class1' => 'path/to/class1.php',
'MyNamespace\\Class2' => 'path/to/class2.php',
'MyNamespace\\Class3' => 'path/to/class3.php',
    public function 
getAutoloaderName(): string
    public function 
getScriptPathstring $class_name ): bool|string
        return static::
CLASSES_MAP[$class_name] ?? false;
Previous chapter
Next chapter