Re: Re: [RFC] Deprecations for PHP 7.1
| From: | Stephen Coakley | Date: | Fri, 04 Mar 2016 18:29:45 +0000 |
| Subject: | Re: Re: [RFC] Deprecations for PHP 7.1 | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91503@lists.php.net to get a copy of this message | ||
On 03/04/2016 07:53 AM, Johannes Schlüter wrote:
On Thu, 2016-02-18 at 19:57 +0000, Andrea Faulds wrote:Good point. Another approach to a polyfill would be to use a closure then: function create_function(string $args, string $code) {I've actually used create_function on occasion for programmatically generating functions (in particular to create a function for each PHP operator), but it's trivially polyfillable, and it'd be better if people were implementing it themselves (and thus knowing it's based on eval()) than using it directly without this knowledge.Just as a detail: You can't create a 100% compatible polyfill to create_function(). create_function() names functions starting with \0. Functions starting with \0 are hidden from get_defined_functions() and possibly other places. php > echo sizeof(get_defined_functions()['user']); 0 php > create_function('', ''); php > echo sizeof(get_defined_functions()['user']); 0 php > eval('function foo() {}'); php > echo sizeof(get_defined_functions()['user']); 1 johannes
return eval("return function($args) { $code };");
}
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo $newfunc(2, M_E) . "\n";
Not a complete polyfill, since $newfunc is a closure and not a string, so you cannot print out the function name. I have no idea if any code relies on the lambda itself being a string though.
--
Stephen