PHP 7.2 has deprecated the create_function() function, leaving developers searching for a compatible alternative. This article tackles this issue by providing a solution that meets the demands of PHP 7.2 while maintaining functionality.
Consider the following code snippet, which employs the create_function() function:
$callbacks[$delimiter] = create_function( '$matches', "return '$delimiter' . strtolower($matches[1]);" );
However, with the deprecation of create_function() in PHP 7.2, this approach requires an update.
The solution lies in anonymous functions, also known as closures. Closures allow the use of parent-scoped variables within the function body, providing a means to access the $delimiter variable:
$callbacks[$delimiter] = function($matches) use ($delimiter) { return $delimiter . strtolower($matches[1]); };
This closure effectively replaces the create_function() call while maintaining the desired functionality. It allows you to continue working seamlessly with PHP 7.2 and beyond.
The above is the detailed content of What's the Best Alternative to PHP's Deprecated `create_function()` in PHP 7.2 and Beyond?. For more information, please follow other related articles on the PHP Chinese website!