2
PHP from the version 5.3
has implemented the resource called funções anônimas
or Closure
.
Its use is this way:
$sort = function ($a, $b) { return $a - $b; };
$array = [1, 3, 2];
usort($array, $sort);
However, when it comes to previous versions, we do not have such a resource, and it is necessary to use two possible amending 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 function
create_function
.
Example:
$lambda = create_function('$a, $b', 'return $a-$b;');
usort($array, $lambda);
In the latter case, my question comes, because this function uses eval
internally. And that’s in the manual.
Example:
create_function('', 'ERRO_DE_SINTAXE_DE_PROPOSITO')
Exit 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
Therefore, on account of using the eval
internally, is its use recommended? Or, in case of versions that do not exist Closure
, should use the function with underline before?