From f82923f96c15e957f4265f21597cd2cdc994482f Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Tue, 7 Feb 2017 16:14:44 +0100 Subject: Replace create_function which will be deprecated in PHP 7.2 --- it.class | 6 ++---- tests/it.t | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/it.class b/it.class index c0d26d7..34f0bf6 100644 --- a/it.class +++ b/it.class @@ -954,13 +954,11 @@ static function date($format = "", $stamp = null) */ static function map($code, $array, $p = null) { - static $cache = array(); - if (is_string($code) && it::match('^[\w:]+$', $code) && is_callable($code)) $code .= '($v)'; - if (!is_callable($func = $code) && !($func = $cache[$code])) - $func = $cache[$code] = create_function('$k,$v', "return $code;"); + if (!is_callable($func = $code) && !function_exists($func = "_it_map_" . md5($code))) + eval("function $func(\$k, \$v) { return $code; }"); # Anonymous functions/closures are slower than this (PHP 7.1) $result = is_array($array) ? $array : iterator_to_array($array); foreach (isset($p['keys']) ? it::filter_keys($array, $p['keys']) : $array as $k => $v) diff --git a/tests/it.t b/tests/it.t index 88e6cae..88fbbb9 100755 --- a/tests/it.t +++ b/tests/it.t @@ -415,7 +415,6 @@ is(it::mod(0, 4), 0); is(it::mod(7, 4), 3); is(it::map('5*$k+$v', array(0 => 1, 1 => 2)), array(1, 7)); -is(it::map(create_function('$k,$v', 'return 5*$k+$v;'), array(0 => 1, 1 => 2)), array(1, 7)); is(it::map(function($k, $v) {return 5*$k+$v;}, array(0 => 1, 1 => 2)), array(1, 7)); is(it::map('strlen', array("aaa", "aa")), array(3, 2)); is(it::map('it::ucfirst', array("aaa")), array("Aaa")); -- cgit v1.2.3