Class loader implementation (PSR-0, PSR-4)
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Class loader

PHP class loader that implements both PSR-0 and PSR-4 autoloading.


  • PSR-0 and PSR-4 autoloading
  • class maps
  • custom suffixes
  • composer bridge
  • debug mode


  • PHP 7.1.0+

Usage examples

Registering prefixes


use Kuria\ClassLoader\ClassLoader;

// load the class loader manually
require '/path/to/src/ClassLoader.php';

// create an instance
$debug = true; // true during development, false in production

$classLoader = new ClassLoader($debug);

// register the autoloader

// PSR-4 prefix
$classLoader->addPrefix('Foo\\Bar\\', 'vendor/foo/bar/src');

    'Kuria\\Error\\' => 'vendor/kuria/error/src',
    'Foo\\Baz\\' => 'example/foo/baz',

// PSR-0 prefix
$classLoader->addPrefix('Example\\FooBar\\', 'vendor/example/foobar', ClassLoader::PSR0);

    'Kuria\\Error\\' => 'vendor/kuria/error/src',
    'Foo_' => 'example/foo',
], ClassLoader::PSR0);

// PSR-4 fallback (empty prefix)
$classLoader->addPrefix('', 'src');

// PSR-0 fallback (empty prefix)
$classLoader->addPrefix('', 'old-code/example', ClassLoader::PSR0);

// single class
$classLoader->addClass('Foo', 'path/to/foo.class.php');

// class map
    'Bar' => 'path/to/bar.class.php',
    'Baz' => 'path/to/baz.class.php',

Using the composer bridge

The ComposerBridge class can be used to initialize autoloading for packages managed by Composer.


use Kuria\ClassLoader\ClassLoader;
use Kuria\ClassLoader\ComposerBridge;

require __DIR__ . '/vendor/kuria/class-loader/src/ClassLoader.php';
require __DIR__ . '/vendor/kuria/class-loader/src/ComposerBridge.php';

$classLoader = new ClassLoader();

ComposerBridge::configure($classLoader, __DIR__ . '/vendor');


Disabling prefixes

If you are using an optimized autoloader, you can pass an optional third parameter to configure() to disable prefixes completely. Only the class maps and files will be loaded.


ComposerBridge::configure($classLoader, __DIR__ . '/vendor', false);

Debug mode

If debug mode is enabled, the following checks are performed after a file is loaded:

  • whether the class was actually found in the file
    • detects wrong or misspelled namespaces or class names
  • whether the class name matches exactly what is defined in the file
    • detects mismatched character case in namespaces or class names or other class name usage, which would cause issues on case-sensitive filesystems

To enable debug mode, call $classLoader->setDebug(true) or pass true to the appropriate constructor argument.