Function create_function Has Been Removed in PHP 8.0

Function create_function Has Been Removed in PHP 8.0

The create_function is a function that allows to create an anonymous function (doesn't have a name). The create_function is based on the eval function which has security issues. Also, it has poor performance and memory usage issues. Since PHP 7.2, create_function has been deprecated and since PHP 8.0 this function has been removed.

PHP 5.3 or newer versions, supports native anonymous functions and should be used instead of create_function.

The create_function has two parameters:

  • $args - arguments of an anonymous function.
  • $code - an anonymous function code.

Let's say we have an anonymous function which has been created using create_function:

<?php

$func = create_function('$x,$y', 'return $x * $y;');
echo $func(4, 5);

A code can be rewritten using a native anonymous function:

<?php

$func = function (int $x, int $y) {
    return $x * $y;
};
echo $func(4, 5);

Leave a Comment

Cancel reply

Your email address will not be published.