通过Include语句访问命名空间类


Access Namespace Classes through Include Statements

考虑一个典型的控制器布局:

namespace app'UserBundle'Controller;
use app'Common'Utils'Utils;
class Controller {
    function someAction() {
        $this->render('path.php');
    }
    function render($path) {
        include ($path);
    }
}

其中path.php使用Utils。是否有一种方法可以在包含时保持相同的名称空间上下文?或者正确的方法是什么?path.php应该只是一个模板文件,控制布局-我宁愿没有名称空间或使用语句。

path.php包含

<div>the time is <?=Utils::niceTime(now())?></div>

您可以为Utils类使用class_alias。

注:namespace没有类似的函数