mishke
mishke

Reputation: 184

use namespace with variable in name

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 ..."

Upvotes: 0

Views: 730

Answers (1)

gaurav
gaurav

Reputation: 1339

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

Upvotes: 2

Related Questions