Should PSR-0 compatible class loader be able to load PEAR libs too?
Yes, PSR-0 is interoperable with the PEAR naming conventions of classes and files.
This is not publicly documented any longer, but it is commonly known.
If you can not load the classes, your autoloader is not PSR-0 compliant.
Double-Check with the specification. You will also find a SplClassLoader
class linked there that is PSR-0 compatible and you can use instead of your own.
A probably better loader is The Symfony2 ClassLoader Component. You can install it easily via Pear or Composer (symfony/class-loader on Packagist).
If you write your own classloader, take care you work with spl_autoload_register
, see
Bonus Function:
To have spl_autoload_register
behaving just like in PHP but with PSR-0 resolution to include-path:
$spl_autoload_register_psr0 = function ($extensions = null) {
$callback = function ($className, $extensions = null) {
if (!preg_match('~^[a-z0-9\\_]{2,}$~i', $className)) {
return;
}
null !== $extensions || $extensions = spl_autoload_extensions();
$extensions = array_map('trim', explode(',', $extensions));
$dirs = array_map('realpath', explode(PATH_SEPARATOR, get_include_path()));
$classStub = strtr($className, array('_' => '/', '\\' => '/'));
foreach ($dirs as $dir) {
foreach ($extensions as $extension) {
$file = sprintf('%s/%s%s', $dir, $classStub, $extension);
if (!is_readable($file)) {
continue;
}
include $file;
return;
}
}
};
return spl_autoload_register($callback);
};
var_dump(get_include_path());
var_dump(spl_autoload_extensions());
var_dump($spl_autoload_register_psr0());