PHP 7.2 function create_function() is deprecated
P粉831310404
P粉831310404 2024-01-16 11:05:38
0
2
403

I am using create_function() in the application below.

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

But as of PHP 7.2.0, create_function() is deprecated.

How to rewrite the above code for PHP 7.2.0?

P粉831310404
P粉831310404

reply all(2)
P粉354948724

I would like to contribute a very simple case I found in a WordPress theme and it seems to work fine:

Has the following add_filter statement:

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

Replace it with:

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

We can see the usage of function(), which is a very typical function creation, instead of using the deprecated create_function() to create a function.

P粉287726308

You should be able to use anonymous functions (aka closures) calls to parent scope $delimiter variables like this:

$callbacks[$delimiter] = function($matches) use ($delimiter) {
    return $delimiter . strtolower($matches[1]);
};
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template