For my application I am using PSR-0 namespaces. Everything works beautiful!

Until I wanted to use Twig as template parser, Twig uses PEAR pseudo namespaces. Like Twig_Loader_Filesystem.

The problem is that when I want to use Twig inside my name-spaced application like this:

<?php
namespace Tact\ViewManager;

class ViewManager {

    public function init()
    {
        $loader = new Twig_Loader_Filesystem($this->templatepath);
        $this->twig = new Twig_Environment($loader);
    }  
}
?>

PHP will tell my autoloader to look for an class named Tact\ViewManager\Twig_Loader_Filesystem

How can I manage to autoload PEAR name-spaced style classes without the PSR-0 namespace of the invoking class?

My autoloader is able to load both PEAR and PSR-0..

Thanks in advance!

有帮助吗?

解决方案

This is because you are in the Tact\ViewManager namespace. The pseudo-namespaced classes are in fact in the global namespace, so you should prefix them with \ to call them:

$loader = new \Twig_Loader_Filesystem($this->templatepath);

If the \ prefix bugs you, you could do this:

namespace Tact\ViewManager;

use Twig_Loader_Filesystem;
use Twig_Environment;

class ViewManager {
    public function init()
    {
        $loader = new Twig_Loader_Filesystem($this->templatepath);
        $this->twig = new Twig_Environment($loader);
    }  
}

其他提示

Try this:

    $loader = new \Twig_Loader_Filesystem($this->templatepath);
    $this->twig = new \Twig_Environment($loader);

This will tell PHP to force namespace\class lookup at "root" level, and if your autoloader is setup to load both namespaces and regular PEAR convention classnames it will work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top