从附加字符串创建新类

时间:2013-02-16 18:18:03

标签: php

是否可以在一行中执行以下操作?例如,像$obj=new "entry_{$type}";谢谢

$class="entry_{$type}";
$obj=new $class();

1 个答案:

答案 0 :(得分:4)

是的,但只有你真的想要它。这有点尴尬:

$obj = new ${!${''}="entry_$type"}();

来自:Code-Golf: one line PHP syntax

PHP希望最终move to a proper parser使用抽象语法树,这可能会加剧目前阻止的语法漏洞,例如new ("entry_$type")()或类似的东西。