Reputation: 1841
I want to use a variable class name in PHP with a prefixed namespace.
Three variants I have tried produce expectation of an identifier error after the last backslash.
(\api\controllers\(new $class()))->{$method}($this->id);
((new \api\controllers\$class()))->{$method}($this->id);
((new \api\controllers\{$class()}))->{$method}($this->id));
How do I make this work?
Upvotes: 0
Views: 74
Reputation: 5098
How about this:
$full_class_name = '\api\controllers\' . $class;
$controller = new $full_class_name();
$controller->{$method}($this->id);
It can probably be shortened somewhat, but doing that here would perhaps make the answer more obscure and less helpful.
Upvotes: 1