0

I am working on something, just practicing and I don't have that much experience. I am using namespaces and I have one functions in class namespaced under 'App\Core' which should include another file with different class and namespaced under 'App\Diff' and return that new class. i.e.

namespace App\Core;

class Test {
    public function testFunc($file) {
        require_once "../folder/" . $file . ".php";
        return new $file();
    }
}

now class inside $file

namespace App\Diff;

class Test2 {
}

I tried something like return

new App\Diff . "\{$file()}";

but I got an error "Class App\Code\App\Diff not found ..."

mishke
  • 185
  • 1
  • 9

1 Answers1

2

As frz3993 mentions that first you have go in global space than you can access \App\Diff and your class . see the manual
Second problem i don't have correct solution but you can see last answer of csga5000 link & manual

namespace App\Core;

class Test {
    public function testFunc($file) {
        require_once "../folder/" . $file . ".php";
         $f = "\\App\\Diff\\".$file ;
         return new $f;
    }
}
Community
  • 1
  • 1
gaurav
  • 1,281
  • 1
  • 13
  • 25