【问题标题】:Parse error: syntax error, unexpected '$class' (T_VARIABLE), expecting identifier (T_STRING) [duplicate]解析错误:语法错误,意外的“$class”(T_VARIABLE),需要标识符(T_STRING)[重复]
【发布时间】:2016-08-20 11:14:11
【问题描述】:

我在变量中有一个类名。我正在创建实例,它给了我错误。下面是代码示例。

use app\controller;

$controller = new controller\$class();

请帮忙。

【问题讨论】:

  • new controller\{$class}();
  • 嗨钻机,它给了我这个错误:解析错误:语法错误,意外的'{',期望标识符(T_STRING)在
  • 我还是没有得到小问题的答案。脱帽stackoverflow....
  • 没有比这更好的了 已经向您展示了如何自己解决问题 阅读 DUP 帖子

标签: php class namespaces


【解决方案1】:

期望你有一个像“Order”这样的类名,如下所示应该可以:

$class = 'Order';
$order = new $class();

你正在使用命名空间app/controller,所以没有必要在你的变量前面写命名空间“controller/”...

应该可以的:

$controller = new $class(); //without "controller/"

【讨论】:

  • 致命错误:在中找不到类“订单”
  • 当然,只要没有这样命名的课程,这将不起作用:D 尝试使用您想要的课程!
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
相关资源
最近更新 更多