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 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 a native anonymous functions and should be used instead of create_function.

The create_function has two parameters:

  • $args – an 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

Your email address will not be published. Required fields are marked *