Autoloader

Of course, automatic loading of classes must not be missing in PHP Jet. It is de facto a modular system of very simple loaders, which includes cache.

The principle is that individual modules are registered in the autoloader system, which do nothing but determine the path to the script of the class - of course, if the loader is relevant for the class. That's all the module needs to do.

Script load, class or interface validation, and caching of the file and class map are already handled by the main system component.

Autoloader and its cache works on the principle of incrementally building a class map as the application runs.

Loader

As already mentioned, the loader is a small simple module that determines the script path for a given class. Let's see an example of an autoloader, i.e. a script: ~/library/Jet/JetAutoloader.php

use Jet\Autoloader_Loader;
use 
Jet\SysConf_Path;

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

You can read more about loaders in the relevant chapter.

But let's say that loaders are divided into two basic groups: library autoloaders and application class autoloaders. Of course, you can create any loader for anything and simply register it. The system is as flexible as you need it to be.

Initialization

Autoloader initialization is done here (unless you specify otherwise :-) ):

namespace JetApplication;

use 
Jet\Autoloader;
use 
Jet\SysConf_Path;

require_once 
SysConf_Path::getLibrary() . 'Jet/Autoloader.php';

Autoloader::initialize();
Autoloader::registerLibraryAutoloaders();
Autoloader::registerApplicationAutoloaders();
Autoloader::initComposerAutoloader();

I'm sure a few important things have not escaped your attention:

  • When initializing Autoloader it is of course necessary to use conventional require.
  • For learning paths, SysConf_Path is used.
  • The initialization script is part of the application space - so you can certainly modify it however you need.

Cache

The purpose of the cache Autoloader is to record the path to the script containing the class when it is first discovered, so that it doesn't have to call loaders next time. This saves hundreds (if not thousands) of calls and all the associated overhead. The cache is of course an important part of Autoloader.

In practice, a map of the paths to the scripts where each class is located is gradually created.

The important thing is that gradually. So only the classes you actually use will be in the map (so the cache is the smallest possible = less data = less trouble for the server).

It is not necessary to devalidate the cache after adding new classes (or the whole library). The cache is simply incrementally updated with new entries.

In practice, you never actually need to devalidate a cache unless you do something really major, move the project to another directory, etc.

Just as the individual Loaders are modular, so is the Autoloader cache itself. Where and in what form the Autoloader stores its cache is a matter of backend. Jet already includes and uses a backend for saving data to files. But if you need to, you can make a backend of your own. (Note: I firmly believe that no one would think of making a backend for an autoloader that will store data in, say, a relational database ... That's not quite the right way to go. This is where maximum simplicity is needed.)

Autoloader cache initialization

Initialization of the autoloader cache can be found in the script ~/application/Init/Cache/Autoloader.php.

The initialization takes the following form:

require_once SysConf_Path::getLibrary() . 'Jet/Autoloader/Cache/Backend/Files.php'

Autoloader_Cache::init( new Autoloader_Cache_Backend_Files() );

Overview of classes

Class Meaning of
Jet\Autoloader Main class. It keeps a list of registered loaders and does the actual automatic loading of classes. It also works with caches.
Jet\Autoloader_Loader Abstract class defining Loader. So every Loader inherits from this class.
Jet\Autoloader_Cache The main class for working with caches. Provides the necessary facade and holds the intent of the backend.
Jet\Autoloader_Cache_Backend Interface defining the backend of the autoloader cache.
Jet\Autoloader_Cache_Backend_Files Default cache backend. Stores data as a single file in the ~/application/data directory
Previous chapter
Jet\Config_Exception
Next chapter
Loader