Home  >  Q&A  >  body text

PHP 7.2 function create_function() is deprecated

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粉831310404P粉831310404251 days ago337

reply all(2)I'll reply

  • P粉354948724

    P粉3549487242024-01-17 10:01:58

    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.

    reply
    0
  • P粉287726308

    P粉2877263082024-01-17 00:49:46

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

    reply
    0
  • Cancelreply