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?
Create a User Defined Function in PHP Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive. Tip: Give the function a name that reflects what the function does!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With