Search code examples
phpclosuresanonymous-function

PHP 7.2 Function create_function() is deprecated


I have used create_function() in my application below.

$callbacks[$delimiter] = create_function(
  '$matches',
   "return '$delimiter' . strtolower(\$matches[1]);"
);

But for PHP 7.2.0, create_function() is deprecated.

How do I rewrite my code above for PHP 7.2.0?


Solution

  • You should be able to use an Anonymous Function (aka Closure) with a call to the parent scoped $delimiter variable, like so:

    $callbacks[$delimiter] = function($matches) use ($delimiter) {
        return $delimiter . strtolower($matches[1]);
    };