PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

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

原创
2016-06-06 20:26:34 2334浏览

请教个问题:
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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。