【问题标题】:conflict of namespace with use statement in phalcon class命名空间与 phalcon 类中的 use 语句冲突
【发布时间】:2015-12-16 22:13:21
【问题描述】:

我对在 index.php 中注册的命名空间与实现关联命名空间的类之间的冲突感到非常困惑。 在 index.php 中:

$loader->registerNamespaces(
    array(
        'Akademik\Controllers' => __DIR__ . $config->application->controllersDir,
        'Akademik\Plugins' => __DIR__ . $config->application->pluginsDir,
        'Akademik\Library' => __DIR__ . $config->application->libraryDir,
        'Akademik\Models' => __DIR__ . $config->application->modelsDir,
))->register();

在我的课堂上:

namespace Akademik\Plugins;

use Phalcon\Mvc\User\Plugin;
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Acl;



class Security extends Plugin
{

   public function __construct($dependencyInjector)
   {
    $this->_dependencyInjector = $dependencyInjector;
   }

   public function getAcl()
   {
      if (!isset($this->persistent->acl)) {

        $acl = new Phalcon\Acl\Adapter\Memory();

当我启动 index.php 时,我得到了:

Fatal error: Class 'Akademik\Plugins\Phalcon\Acl\Adapter\Memory' not found in C:\nginx\html\app\plugins\Security.php on line 27

我是 phalcon 的新手,还有命名空间,任何帮助将不胜感激。提示

【问题讨论】:

    标签: class namespaces


    【解决方案1】:

    您没有正确的命名空间转义,并且适配器未设置在 use 语句下,这就是它放置在 Akademik\Plugins\ 命名空间下的原因。

    这里有两种解决方法。

    最简单的选择

    // Go to This line 
    $acl = new Phalcon\Acl\Adapter\Memory();
    

    因此,您必须将其更改为此,以添加到全局命名空间:

    // Add an Escape "\" before Phalcon
    $acl = new \Phalcon\Acl\Adapter\Memory();
    

    其他选项

    否则,你可以做一个类似顶部的use语句:

    // Add this to the top (Alias is optional, otherwise it's new Memory())
    use Phalcon\Acl\Adapter\Memory as AclMemory;
    
    ... 
    
    // Create the instance this way
    $acl = new AclMemory();
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多