New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

actra/autoloader

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

actra/autoloader

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

Source
Packagist
Version
v1.1.0
Version published
Total downloads
79
Maintainers
1
Created
Source

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

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).

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

FAQs

Package last updated on 15 Mar 2026

Related posts