Class loader that triggers a callback when using old class name

Installs: 2

Dependents: 0

Stars: 1

Watchers: 1

Language: PHP

v1.0 2014-06-01 07:50 UTC


Build Status

Simple loader for loading old/renamed classes with onClassLoaded event useful e.g. for logging or noticing that old class name is used. The loader uses class_alias() function to alias new class name with the old one.


require: "icaine/renamed-class-loader": "~1.0"


//registering classes
$loader = new iCaine\RenamedClassLoader([
    'Old\\Class\\Name' => 'New\\Class\\Name'

//or like this
$loader->registerClass('Old\\Class\\Name', 'New\\Class\\Name');

//or this way
    'Old\\Class\\Name' => 'New\\Class\\Name'

//we can register a callback(s) when the loader successfully loads old class
$loader->onClassLoaded[] = function($oldName, $newName) {
    trigger_error("Old class name used: '$oldName'. Use new name '$newName' instead.", E_USER_DEPRECATED);

//now lets register the loader (uses spl_autoload_register function)

Note: $loader->register() should be called after your loader is registered because RenamedClassLoader uses class_alias() function that will try to load new class name first and then alias it with the old name.