Deprecated: Function create_function() is deprecated [duplicate]

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, the create_function() is deprecated.

How to fix my code above on PHP 7.2.0.


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]);
};

I would like to contribute with a very simple case I found in a Wordpress Theme and seems to work properly:

Having the following add_filter statement:

add_filter( 'option_page_capability_' . ot_options_id(), create_function( '$caps', "return '$caps';" ), 999 );

Replace it for:

add_filter( 'option_page_capability_' . ot_options_id(), function($caps) {return $caps;},999);

We can see the usage of function(), very typical function creation instead of a deprecated create_function() to create functions. Hope it helps.