Question

So this is my question, I wrote my own class autoloader that fallows PSR-0 standard. That's easy. Now I have my PHP MVC Frameowrk and I would like to use Twig, but Twig fallows PEAR naming standard that use _ instead of \.

Now is it true that PSR-0 Autoloaders should also be able to load PEAR libs? Since inside those Autoloaders _ is transformed into \? My loader can't load Twig if I register it, but it may be I made a mistake somewhere.

Should PSR-0 compatible class loader be able to load PEAR libs too?

Was it helpful?

Solution

PSR-0 should be able to work with both full-qualified namespaces and underscored classes. All you should need to do is replace the underscores with a directory seperator. A good example would be:

function autoload($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strripos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
}

Hope this helps!

OTHER TIPS

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top