PHP How to instantiate a class returned as a string from a method call

How to call something like this:

$instance = new ($b->method($id))();

where method(int $id): stringdoes the class name return?

The construction above gives me a syntax error, but this is normal:

$className = $b->method($id);
$instance = new $className();

I'm just wondering if and how this can be done. I was surprised that the brackets cannot indicate that the contents of the brackets $b->method($id)should be executed first and the resulting string used to instantiate the object. I probably won't use it in production code, but I'm still interested.

+6
source share
1 answer

PHP, "new" . () languaje, .

+1

All Articles