Sunday, 7 October 2018

PHP 7.2 - How to create anonymous function dynamically when having function body in a string

How do I create an anonymous function dynamically when I have function body in a string.

For e.g.

$user = "John Doe";
$body = "echo 'Hello' . $user;";

$myFunct = function($user) {$body}; // How do I have function body here from string.

$myFunct($user);

Any help would be much appreciated.

P.S. I'm looking for a replacement for create_function() functionality which was there in prior versions of PHP. Just like in create_function() where we could pass the function body as a string, I would like to define anonymous function's body off the string variable.



from PHP 7.2 - How to create anonymous function dynamically when having function body in a string

No comments:

Post a Comment