user3788675
user3788675

Reputation: 47

php namespaces conflicting with composer namespaces

In my project I have a folder for modules where they can have composer packages The namespace for the module I'm making is Modules\View\View for Modules/View/View.php file

this is the directory layout

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

when I do this in 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);
}

I get an error: Fatal error: Class 'Modules\View\Twig_Loader_Filesystem' not found in Modules/View/View.php on line 12

So I assumed this is because the View.php file has a namespace and I went to /Modules/View/vendor/composer/autoload_namespaces.php and changed

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

to

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

and it still doesn't work, however I'm still 100% sure it's because of the namespace because if you try to load it in a file without a namespace it works but as soon as you add it it gives that error

Upvotes: 2

Views: 764

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35337

What is the namespace of Twig_Loader_Filesystem?

If it does not have a namespace, you can load it using the root namespace (\):

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

You can also use the use keyword:

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);
  }

}

Upvotes: 1

Related Questions