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

4
  • What happen if you put another backslash at the front like new \App\Diff ? Commented Feb 26, 2017 at 17:19
  • Class App\Diff not found. Commented Feb 26, 2017 at 18:01
  • How do you instantiate the Test class and call the testFunc() ? Commented Feb 26, 2017 at 18:14
  • I call it from another class. use App\Core\Test; and call it from within a function public function function callTest() { $a = $this->b->testFunc('ClassName'); } and b is an instance of Test class Commented Feb 26, 2017 at 18:21

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That actually worked. Thanks, I'll read it and continue with my project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.