This tutorial is based on the basic plugin setup. You should be familiar with it before reading this tutorial.
There is a Pluginkit example plugin with an autoloader as well. You can find its code in the
2-autoloader branch of the Pluginkit.
If you want to follow along, you can download a ZIP file of that branch or get it via Composer:
If your plugin uses PHP classes, you should use one PHP file per class to keep everything organized. Those files need to be loaded somehow.
The most efficient way to load PHP classes is to use an autoloader. It will dynamically load your PHP files once they are needed for the first time, which is great for performance.
Kirby ships with the
load helper, which is a super simple class autoloader.
You can call it in your plugin's
index.php with the list of your classes to load:
Once any of the autoloaded classes is first used, Kirby will automatically load its PHP file.