PHP Autoloader
A simple, lightweight, and efficient PHP Autoloader that supports PSR-4, PSR-0, filesystem caching, and custom file suffix lists. This autoloader is designed to be easy to integrate into any PHP project without the overhead of complex dependency management if you need a standalone solution.
Features
- PSR-4 and PSR-0 Support: Load classes according to modern and legacy standards.
- Filesystem Caching: Boost performance by caching class-to-file mappings in a PHP file.
- Custom Suffixes: Supports multiple file extensions (e.g.,
.php, .inc.php, .class.php).
- Namespace Prefixes: Easily map namespaces or prefixes to specific directories.
- Lightweight: Zero external dependencies.
Installation
Via Composer (Recommended)
To install the autoloader via Composer, run:
composer require actra/autoloader
Standalone (Manual)
If you're not using Composer, simply include the source files in your project:
require_once 'src/Autoloader.php';
Basic Usage
PSR-4 Autoloading
use actra\autoloader\Autoloader;
use actra\autoloader\AutoloaderPath;
$autoloader = Autoloader::register();
$autoloader->addPath(autoloaderPath: new AutoloaderPath(
path: __DIR__ . '/src',
prefix: 'MyProject\\'
));
PSR-0 Autoloading (Legacy)
use actra\autoloader\Autoloader;
use actra\autoloader\AutoloaderPath;
use actra\autoloader\AutoloaderPathMode;
$autoloader = Autoloader::register();
$autoloader->addPath(autoloaderPath: new AutoloaderPath(
path: __DIR__ . '/lib',
prefix: 'Legacy_',
autoloaderPathMode: AutoloaderPathMode::PSR0
));
Advanced Features
Filesystem Caching
By default, the autoloader uses a local cache file at src/cache/autoloader.php. This significantly speeds up class loading in production environments by leveraging OPcache. The cache directory is automatically created if it doesn't exist.
To use a custom cache location, provide a path when registering:
$autoloader = Autoloader::register(cacheFilePath: __DIR__.' /cache/autoloader.php');
The cache is automatically updated and saved when the script finishes execution (using a destructor).
Custom File Suffixes
If your project uses non-standard file extensions, you can specify them in the AutoloaderPath constructor:
$autoloader->addPath(autoloaderPath: new AutoloaderPath(
path: __DIR__ . '/src',
prefix: 'App\\',
fileSuffixList: ['.php', '.class.php', '.inc']
));
Requirements
- PHP 8.2 or higher (Uses
readonly classes and enums)
License
This project is licensed under the MIT License - see the LICENSE file for details.
© Actra AG - https://www.actra.ch