When programming a namespace problem, first run the code: {code ...} I tried to dynamically instantiate the class of another namespace through the variable in the getClass method, but the "Classnotfound" error was triggered. I think the $ class variable problem occurs, so I want to test... when programming such a namespace problem, first go to the code:
namespace Test { class Test {}}namespace Chou { use Test\Test; class Mee { public $class = 'Test'; public function getClass() { return new $this->class(); } } $tmp = new Mee(); $tmp->getClass();}
I tried to dynamically instantiate the Class of another namespace through the variable in the getClass method, but the "Class not found" error was triggered. I think the $ class variable is a problem, so I simplified the test code:
namespace Chou { use Test\Test; $test = 'Test'; $tmp = new $test();}
The error is as expected. I know that it can be solved in a fully qualified way, but I still want to understand what the principle is and hope to solve it.
I neglected myself. I found namespaces and dynamic language features in the manual..
Reply content:
When programming, we encounter such a namespace issue. First, we should go to the code:
namespace Test { class Test {}}namespace Chou { use Test\Test; class Mee { public $class = 'Test'; public function getClass() { return new $this->class(); } } $tmp = new Mee(); $tmp->getClass();}
I tried to dynamically instantiate the Class of another namespace through the variable in the getClass method, but the "Class not found" error was triggered. I think the $ class variable is a problem, so I simplified the test code:
namespace Chou { use Test\Test; $test = 'Test'; $tmp = new $test();}
The error is as expected. I know that it can be solved in a fully qualified way, but I still want to understand what the principle is and hope to solve it.
I neglected myself. I found namespaces and dynamic language features in the manual..
The manual is written, and namespace is used in variables.Must use a fully qualified name.
Http://php.net/manual/zh/language.nam...