Search by

actra / autoloader

JayQ1982

A simple, lightweight, and efficient PHP Autoloader supporting PSR-4, PSR-0, and filesystem caching with OPcache support.

Package info

github.com/Actra-AG/autoloader

pkg:composer/actra/autoloader

Statistics

Installs: 249

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.2.0 2026-10-09 11:04 UTC

This package is auto-updated.

Last update: 2026-10-09 11:04:16 UTC


README

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;

// Register the autoloader
$autoloader = Autoloader::register();

// Add a path for a namespace
$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). It is written atomically and invalidated in OPcache, so concurrent requests always include a complete cache file.

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.5 or higher

License

This project is licensed under the MIT License - see the LICENSE file for details.

© Actra AG - https://www.actra.ch