【发布时间】:2018-08-13 08:37:11
【问题描述】:
我正在尝试使用动态路径启动对象。我有带有模型名称的变量$model。
$model = "foo";
$class = new \Path\To\$model();
我得到了错误
Parse error: syntax error, unexpected '$model' (T_VARIABLE), expecting identifier (T_STRING)
如果我尝试$class = new \Path\To\{$model}();
我收到错误
Parse error: syntax error, unexpected '{', expecting identifier (T_STRING)
当我尝试时
namespace \App\Models
$class = new $model();
我收到错误Class 'foo' not found
当我尝试$class = new \Path\To\foo(); 时,它可以工作。
有什么想法吗?
【问题讨论】:
标签: php laravel class object model