Deprecation of create_function() in PHP 7.2 and Alternative Using Anonymous Functions

Dec 03, 2025 · Programming · 10 views · 7.8

Keywords: PHP | create_function | anonymous_function | closure | deprecation

Abstract: This article discusses the deprecation of the create_function() function in PHP 7.2, explains the reasons behind it, and provides a detailed guide on how to replace it with anonymous functions or closures. It includes code examples and best practices for modern PHP development.

Introduction

In PHP 7.2, the create_function() function has been deprecated, signaling a shift towards more secure and efficient coding practices. This article explores the implications of this change and demonstrates how to adapt existing code to use anonymous functions.

Why create_function() is Deprecated

The create_function() function was introduced in earlier versions of PHP to create functions dynamically. However, it has several drawbacks, including security vulnerabilities due to string evaluation and performance issues. In PHP 7.2, it is marked as deprecated to encourage the use of anonymous functions, which are more robust and integrated into the language.

Alternative Using Anonymous Functions

Anonymous functions, also known as closures, provide a modern and safer way to define functions on the fly. They allow for better variable scoping and avoid the pitfalls of string evaluation.

Rewriting Code with Anonymous Functions

Consider the original code that uses create_function():

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

To rewrite this for PHP 7.2, use an anonymous function with the use statement to capture the $delimiter variable from the parent scope:

$callbacks[$delimiter] = function($matches) use ($delimiter) {
    return $delimiter . strtolower($matches[1]);
};

This approach eliminates the need for string evaluation and makes the code more readable and maintainable.

Additional Examples

In other contexts, such as WordPress themes, similar replacements can be made. For instance, replacing:

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

with:

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

This demonstrates the versatility of anonymous functions in various PHP applications.

Conclusion

With the deprecation of create_function() in PHP 7.2, developers are encouraged to adopt anonymous functions for dynamic function creation. This not only aligns with modern PHP standards but also enhances code security and performance. By using closures and the use statement, existing code can be easily upgraded to be compatible with newer PHP versions.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.