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
  • 58,060
  • 25
  • 98
  • 150
user1032531
  • 24,767
  • 68
  • 217
  • 387

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
  • 48,794
  • 16
  • 117
  • 146