PHP 7.2 Deprecation: Alternative to create_function()
In PHP 7.2, the create_function() function has been deprecated. This can leave developers wondering how to update their code that utilizes this function.
Consider the following code example:
$callbacks[$delimiter] = create_function( '$matches', "return '$delimiter' . strtolower($matches[1]);" );
With the deprecation of create_function(), a suitable alternative is to use an Anonymous Function (or Closure):
$callbacks[$delimiter] = function($matches) use ($delimiter) { return $delimiter . strtolower($matches[1]); };
In this example, the $delimiter variable is passed into the Closure's scope using the use() statement. This ensures that the Closure can access the variable even though it is defined outside of the Closure itself.
The above is the detailed content of PHP 7.2 Deprecated `create_function()`: What's the Best Alternative?. For more information, please follow other related articles on the PHP Chinese website!