当前位置:Gxlcms > PHP教程 > yaf.use_namespace=1的情况下,controller怎么使用namespace

yaf.use_namespace=1的情况下,controller怎么使用namespace

时间:2021-07-01 10:21:17 帮助过:4人阅读

请教个问题:
yaf. use_namespace=1的情况下,controller怎么使用namespace?

比如:Index.php的内容

use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
class IndexController extends Controller_Abstract
{
    public function indexAction()
    {
        Dispatcher::getInstance()->disableView(0);
        echo 'Great,It Works!';
    }
}

没法加namespace了,文件名是Index.php

路过的怎么处理的,说说

回复内容:

请教个问题:
yaf. use_namespace=1的情况下,controller怎么使用namespace?

比如:Index.php的内容

use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
class IndexController extends Controller_Abstract
{
    public function indexAction()
    {
        Dispatcher::getInstance()->disableView(0);
        echo 'Great,It Works!';
    }
}

没法加namespace了,文件名是Index.php

路过的怎么处理的,说说

我也想知道答案。 原本想

一直报错。去掉namespace App\Controllers就行。不知道怎么办了。

php.ini 开启

yaf.use_namespace = 1
yaf.use_spl_autoload = 1

定义加载器 可以在公用function中

/** 自动加载器 modules */
function autoload_modules($class){
    $class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
    
    ...

    $file = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'xxxx' . $class . '.php';
    if( file_exists($file) )
        Loader::import($file);
}

然后在 Bootstrap 文件中用 spl_autoload_register 自定义加载器

不过开启 use_spl_autoload 会影响效率

开启的情况下, Yaf在加载不成功的情况下, 会继续让PHP的自动加载函数加载, 从性能考虑, 除非特殊情况, 否则保持这个选项关闭

use_spl_autoload

也是一直报错,原来是index里加了 namesapce App\Controllers

人气教程排行