0

我正在尝试做这样的事情:

$controller = new $controller . 'Controller';

如果是,这将创建PagesController该类的一个新实例。但是,我收到此错误:$controllerPages

Fatal error: Class 'Pages' not found in C:\xampp\htdocs\test\application\app.php on line 25

该类名为PagesController.

我以前在 PHP 中看到过此操作,但我找不到任何文档。

4

3 回答 3

4

做这个

$controller = $controller . 'Controller';
$controller = new $controller();
于 2012-09-08T17:02:11.443 回答
0

尝试:

$controller .= 'Controller';
$controller = new $controller();

文档:
构造函数和析构函数
变量

于 2012-09-08T17:03:20.103 回答
0

上述答案仅在您指定完整命名空间时才有效,无论您是否使用过usenamespace之前使用过。

use \App\Article\Health as Health;

$article = new Health; // works fine

$classname = "Health";
$article = new $classname(); // Class 'Health' not found.

$c = "\\App\\Article\\$classname";
$article = new $c(); // works fine
于 2014-12-29T18:38:14.470 回答