yaf icon indicating copy to clipboard operation
yaf copied to clipboard

如果使自己的文件也使用namespace

Open 9466 opened this issue 9 years ago • 7 comments

yaf自己的类可以开启namespace调用,请问我自己的程序文件也希望使用namespace来规划,如:

getAppDirectory(); echo $this->_request->getActionName(); $this->_view->assign('word', 'Hello World'); } } 现在是运行不了的,请问怎么做比较好呢

9466 avatar Mar 25 '15 09:03 9466

1、修改yaf的配置项

yaf.use_namespace = 1
yaf.use_spl_autoload = 1

2、在你的Bootstrap文件中引入composer的autoload.php就可以。

<?php
use \Yaf\Bootstrap_Abstract;
use \Yaf\Dispatcher;
use \Yaf\Loader;

/**
 * Class Bootstrap
 */
class Bootstrap extends Bootstrap_Abstract
{
    /**
     * @param Dispatcher $dispatcher
     */
    function _initComposerAutoload(Dispatcher $dispatcher)
    {
        $autoload = APPLICATION_PATH . '/vendor/autoload.php';
        if (file_exists($autoload)) {
            Loader::import($autoload);
        }
    }
}

JianfuLi avatar Apr 21 '15 09:04 JianfuLi

开启了spl_autoload效率会受影响。希望Yaf本身能支持项目引入命名空间。

yqt avatar Jul 28 '15 19:07 yqt

鸟哥好像对命名空间没什么感觉,还停留在pecl时代

lovelock avatar Dec 13 '15 02:12 lovelock

@JianfuLi 感谢你提供的方法。

shyandsy avatar Jan 04 '16 21:01 shyandsy

@JianfuLi 多谢你提供的方法。

creazy412 avatar May 30 '18 07:05 creazy412

已经解决 参考这里 https://github.com/letwang/HookPHP/blob/master/app/admin/models/Rbac/Role.php

letwang avatar Dec 06 '18 02:12 letwang

1、修改yaf的配置项

yaf.use_namespace = 1
yaf.use_spl_autoload = 1

2、在你的Bootstrap文件中引入composer的autoload.php就可以。

<?php
use \Yaf\Bootstrap_Abstract;
use \Yaf\Dispatcher;
use \Yaf\Loader;

/**
 * Class Bootstrap
 */
class Bootstrap extends Bootstrap_Abstract
{
    /**
     * @param Dispatcher $dispatcher
     */
    function _initComposerAutoload(Dispatcher $dispatcher)
    {
        $autoload = APPLICATION_PATH . '/vendor/autoload.php';
        if (file_exists($autoload)) {
            Loader::import($autoload);
        }
    }
}

使用了这个方法后报错: [1] 86596 segmentation fault /usr/local/Cellar/[email protected]/7.3.24/bin/php cli.php

ghxter avatar Feb 24 '21 14:02 ghxter