【问题标题】:php namespaces conflicting with composer namespacesphp 命名空间与作曲家命名空间冲突
【发布时间】:2015-05-05 01:27:55
【问题描述】:

在我的项目中,我有一个模块文件夹,它们可以在其中包含作曲家包 我正在制作的模块的namespace 是 Modules\View\View for Modules/View/View.php 文件

这是目录布局

Modules
-View
--View.php
--vendor
---autoload.php

当我在 View.php 中这样做时

public function __construct(){
    require_once __DIR__.'/vendor/autoload.php';
    $this->loader = new Twig_Loader_Filesystem('design');
    $this->twig = new Twig_Environment($this->loader);
}

我收到一个错误: 致命错误:第 12 行的 Modules/View/View.php 中未找到 Class 'Modules\View\Twig_Loader_Filesystem'

所以我认为这是因为 View.php 文件有一个命名空间,我去了 /Modules/View/vendor/composer/autoload_namespaces.php 并更改了

'Twig_' => array($vendorDir . '/twig/twig/lib')

'Modules\View\Twig_' => array($vendorDir . '/twig/twig/lib')

它仍然不起作用,但是我仍然 100% 确定这是因为 namespace,因为如果您尝试将其加载到没有 namespace 的文件中,它会起作用,但只要您添加它它给出了那个错误

【问题讨论】:

    标签: php namespaces composer-php


    【解决方案1】:

    Twig_Loader_Filesystem 的命名空间是什么?

    如果它没有命名空间,你可以使用根命名空间(\)加载它:

    $this->loader = new \Twig_Loader_Filesystem('design');
    

    您也可以使用use 关键字:

    namespace Modules\View;
    
    use Twig_Loader_Filesystem;
    use Twig_Environment;
    
    class View {
    
      public function __construct(){
        require_once __DIR__.'/vendor/autoload.php';
        $this->loader = new Twig_Loader_Filesystem('design');
        $this->twig = new Twig_Environment($this->loader);
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 2020-04-23
      • 2012-09-10
      • 2013-01-26
      相关资源
      最近更新 更多