0

Is it possible to do the following in one line? For instance, something like $obj=new "entry_{$type}"; Thanks

$class="entry_{$type}";
$obj=new $class();
tereško
  • 57,247
  • 24
  • 95
  • 149
user1032531
  • 22,993
  • 61
  • 191
  • 346

1 Answers1

4

Yes, but only if you really want it. It's a bit awkward:

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

From: Code-Golf: one line PHP syntax

PHP wants to eventually move to a proper parser that uses an Abstract Syntax Tree, which might tighten up syntax holes that currently prevent, for example, new ("entry_$type")() or something like that.

Community
  • 1
  • 1
Boann
  • 47,128
  • 13
  • 114
  • 141