On 03/04/2016 07:53 AM, Johannes Schlüter wrote:
On Thu, 2016-02-18 at 19:57 +0000, Andrea Faulds wrote:
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
Good point. Another approach to a polyfill would be to use a closure then:
function create_function(string $args, string $code)
{
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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php