Does there exist a one-liner (yes, I love them) to create new instance of a class based on the returned string of a function?
$obj = new {functionThatReturnsAStringValue()}();
I understand what you want, but i think you can do it that way:
$obj = ($class = functionThatReturnsAStringValue()) ? new $class() : null;
function getObject()
{
return 'DateTime';
}
$datetime = call_user_func(function ($obj) { return new $obj; }, getObject());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With