Create a new instance of the class from the return value of the function - single line

Is there a one-line (yes, I love them) to create a new instance of the class based on the returned function string?

$obj = new {functionThatReturnsAStringValue()}(); 
+4
source share
2 answers

I understand what you want, but I think you can do it this way:

 $obj = ($class = functionThatReturnsAStringValue()) ? new $class() : null; 
+6
source
 function getObject() { return 'DateTime'; } $datetime = call_user_func(function ($obj) { return new $obj; }, getObject()); 
+2
source

All Articles