PHP:在PSR-0命名空间类中自动加载PEAR命名空间类时发生冲突 [英] PHP: Autoloading PEAR namespaced classes within PSR-0 namespaced classes conflict
问题描述
对于我的应用程序,我正在使用PSR-0命名空间.一切正常!
For my application I am using PSR-0 namespaces. Everything works beautiful!
在我想使用Twig作为模板解析器之前,Twig使用PEAR伪命名空间.就像Twig_Loader_Filesystem.
Until I wanted to use Twig as template parser, Twig uses PEAR pseudo namespaces. Like Twig_Loader_Filesystem.
问题是,当我想像这样在名称分隔的应用程序中使用Twig时:
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会告诉我的自动加载器寻找一个名为Tact\ViewManager\Twig_Loader_Filesystem
PHP will tell my autoloader to look for an class named Tact\ViewManager\Twig_Loader_Filesystem
如何在没有调用类的PSR-0名称空间的情况下自动加载PEAR名称分隔的样式类?
我的自动加载器能够加载PEAR和PSR-0.
My autoloader is able to load both PEAR and PSR-0..
提前谢谢!
推荐答案
这是因为您在Tact\ViewManager
命名空间中.
伪命名间隔的类实际上位于全局命名空间中,
所以您应该在它们前面加上\
来调用它们:
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);
}
}
这篇关于PHP:在PSR-0命名空间类中自动加载PEAR命名空间类时发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!