Home > Backend Development > PHP Tutorial > 为什么PHP new 可变class时提示异常

为什么PHP new 可变class时提示异常

WBOY
Release: 2016-06-13 12:20:43
Original
1162 people have browsed it

为什么PHP new 可变class时提示错误?

namespace Demo;<br />use Demo\Controller\DemoController;<br /><br />$c_str=$_GET['c'];<br />$c_name="DemoController";<br /><br />require 'Controller/'.$c_name.'.php';<br /><br />$method=$_GET['a'];<br /><br />$demo=new $c_name;  // 我在这块手工填写: $demo=new DemoController; 是没问题的,换成$c_name 就报错<br />$demo->$method();
Copy after login



------解决思路----------------------
namespace Demo;
use Demo\Controller\DemoController;

确定下路径是否正确
------解决思路----------------------
这不就对了吗?
DemoController 在 Demo\Controller 命名空间下


$c_name="Controller\DemoController";
也是可以的

另外请注意:
use Demo\Controller\DemoController;
是使用 Demo\Controller\DemoController 命名空间
而你实际并不存在这个命名空间,因为 DemoController 是类
你若写作
use Demo\Controller\DemoController as DemoController;
就可以 $c_name="DemoController"; 了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template