如何解决在PHP或Cakephp 3中使用variabel作为类名
我正在尝试从另一个命令运行一个命令数组。类似于cronjob的东西,但最终用户具有视图编辑添加选项。 这是可行的:
$fooCommand = new \App\Command\fooCommand();
$barCommand = new \App\Command\barCommand();
但是我需要的是:
$classes = [
"foo","bar"
];
foreach ($classes as $class) {
$otherCommand = new '\\App\\Command\\' .$class. Command();
# code...
}
类似的东西。在数组中迭代并启动类。实际上从数据库运行命令。
解决方法
您需要分隔您的类名并使用variables variable
foreach ($classes as $class) {
$_class = "\\App\\Command\\{$class}Command";
$class_name = $class . 'Command';
$$class_name = new $_class;
# code...
}
,
您可以这样做:
foreach ($classes as $class) {
$classWithNamespace = "\\App\\Command\\{$class}Command";
$otherCommand = new $classWithNamespace();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。