PHP 7.2 Function create_function() is deprecated

PHP 7.2 Function create_function() is deprecated

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Migrating create_function() to Anonymous Functions in PHP 7.2+

If you are maintaining legacy codebases or migrating older scripts, you will inevitably encounter the use of create_function(). This built-in function allowed developers to define anonymous functions directly within expressions, effectively creating closures on the fly without needing a separate definition block. While this syntax offers brevity, it has significant drawbacks regarding security and performance that led to its deprecation in PHP 7.2.0.

The specific example provided shows a common pattern used for string manipulation callbacks:

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

In this snippet, create_function takes a parameter list ('$matches') and a function body as a string. The resulting closure is stored in an array keyed by a delimiter. While functional, this approach constructs the code at runtime using string concatenation and eval-like behavior under the hood. This makes it susceptible to security vulnerabilities if the dynamic arguments are not strictly sanitized, and it prevents the engine from applying standard optimizations for anonymous functions.

To migrate this code for PHP 7.2 and later, you must replace create_function with a standard closure definition. The primary challenge in migration is ensuring that variable scope remains identical. In the original snippet, $delimiter is an external variable captured by the function's body string. When rewriting this as a modern closure, you should use the use keyword to capture the current value of $delimiter from the parent scope.

Here is the direct replacement:

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

This version removes the need for string parsing and variable interpolation inside a code block. The closure now accepts $matches as its argument, just like the original, but it explicitly tells PHP to bind the current value of $delimiter when the function is invoked. This ensures that if $delimiter changes between assignments or within the loop populating $callbacks, the behavior matches the intended logic without relying on string-based scope resolution.

There are specific edge cases where create_function was used because it allowed accessing variables in a slightly different context than standard closures, though often those differences were bugs rather than features. The modern closure syntax is generally safer and more performant. It allows the PHP engine to optimize the function body before execution, whereas create_function deferred this until runtime.

When performing this migration, pay close attention to how variables are passed. In the original code, $matches[1] refers to a specific element of a captured group from a regex match. Ensure that your replacement logic preserves this exact indexing and data type expectations. If the original code relied on global scope access within the string provided to create_function, you must explicitly import those globals or pass them as arguments in the closure definition.

Replacing deprecated functions is often part of a broader modernization effort. While this specific change improves security and performance, it also aligns your codebase with current best practices for defining callbacks. Tools like LaravelCompany often recommend auditing legacy PHP applications for such deprecated constructs to ensure long-term stability and compatibility with newer PHP versions.

Our network