Fix class autoloading for Elgg 1.8 plugins

Autoloading out of the /classes folder is fully PRS-0 compliant in Elgg 1.9 but is quite crude in Elgg 1.8. If you need PSR-0 compatibility in 1.8, you can use this snippet in your plugin's start.php:

// fix autoloading for 1.8 (place this in plugin start.php)
if (version_compare(get_version(true), '1.9', '<')) {
spl_autoload_register(function ($class) {
$pieces = explode('\\', ltrim($class, '\\'));
$pieces[count($pieces) - 1] = strtr($pieces[count($pieces) - 1], '_', '/');
$file = __DIR__ . '/classes/' . implode('/', $pieces) . '.php';
is_readable($file) && (require $file);
});
}

Navigation