PHP from version 5.3
has implemented the feature called funções anônimas
or Closure
.
Its use goes like this:
$sort = function ($a, $b) { return $a - $b; };
$array = [1, 3, 2];
usort($array, $sort);
However, when it comes to the previous versions, we do not have a feature of this type, needing to use two possible alterative features:
- Create functions initialized by
_
to identify that it is "temporary" or is only for callback.
Example:
function _sort($a, $b)
{
return $a - $b;
}
usort($array, '_sort');
- Use the
create_function
function.
Example:
$lambda = create_function('$a, $b', 'return $a-$b;');
usort($array, $lambda);
In the latter case, my question comes in because this function uses eval
internally . And that's in the manual.
Example:
create_function('', 'ERRO_DE_SINTAXE_DE_PROPOSITO')
Output will be:
PHP Parse error: syntax error, unexpected '}' in phar: ///usr/local/bin/psysh/src/Psy/ExecutionLoop/Loop.php (76): eval () 'd code (1): runtime-created function on line 1
So, on account of using eval
internally, is it recommended to use it? Or, for versions that do not exist Closure
, should I use the function with the underline before?