我正在尝试使用动态路径启动一个对象。我有$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();
它时。
有任何想法吗?
尝试:
$class = "\Path\To\foo";
$object = new $class();
或者:
use Path\To\foo;
$class = foo::class;
$object = new $class();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句