replacing create_function() with something else for PHP8

replacing create_function() with something else for PHP8

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

Stefan Bogdanescu

Founder & Senior Architect

Replacing create_function() in PHP 8

The create_function() function has been removed from PHP 8, marking a significant shift in how developers should handle dynamic callback generation. If you are encountering a fatal error stating that create_function() is undefined, it indicates your codebase still relies on this legacy tool. This function was designed to create anonymous functions at runtime using a syntax reminiscent of Perl or C-style callbacks, but its removal forces a transition to modern PHP closures and standard anonymous functions.

The specific line causing the issue in many legacy projects looks like this:

$callback_2 = create_function('$matches', 'return "[" . str_replace("|", "", $matches[1]) . "]";');

In this example, a string of code defining a logic to manipulate matched strings is being converted into an executable function. The arguments $matches are passed as the variable list, and the second argument contains the body of the function. While this was convenient for quickly gluing together logic without declaring a named function or a closure, it suffered from poor readability, scoping limitations, and the inability to access outer scope variables directly in many contexts.

The most direct replacement is using an anonymous function with arrow syntax or standard closure syntax. The logic inside your create_function call needs to be moved into the function body explicitly. Here is how you translate that specific line:

// Original legacy code
$callback_2 = create_function('$matches', 'return "[" . str_replace("|", "", $matches[1]) . "]";');

// Modern PHP 8 replacement
$callback_2 = function ($matches) {
    return '[' . str_replace('|', '', $matches[1]) . ']';
};

The key difference lies in how variables are handled. In the legacy create_function, the variable list $matches was defined as a global scope parameter within the generated function. In the modern version, you must explicitly declare $matches as an argument in the function signature. The body of the function remains largely the same, but you gain access to proper scoping rules and type hinting capabilities if needed later.

One common pitfall for developers new to this transition is attempting to wrap the entire logic in a block without arguments, or failing to close the function with a semicolon before assigning it to the variable. The syntax $callback_2 = function() { ... }; creates a closure that can be passed around like any other callable.

If your original create_function relied on global variables rather than the first argument, you must ensure those globals are accessible or passed into the closure. In PHP 8, closures have strict scoping rules; they do not automatically inherit global scope unless explicitly declared with the use keyword. For instance, if your logic depended on a global constant $BASE_URL, you would need to capture it:

$callback_2 = function ($matches) use ($BASE_URL) {
    return '[' . $BASE_URL . str_replace('|', '', $matches[1]) . ']';
};

This explicitness makes the code harder to accidentally break and easier for other developers to understand. While create_function() allowed you to bypass these rules with a string of text, the modern approach enforces clarity.

For complex refactoring tasks or if you are working within the Laravel ecosystem, you might also consider using helper classes or traits to encapsulate this logic, reducing the need for inline anonymous functions in controller logic. However, for simple replacements like the one shown above, an arrow function often provides a concise alternative:

$callback_2 = fn ($matches) => '[' . str_replace('|', '', $matches[1]) . ']';

Arrow functions are particularly useful when you only need to return a value, as they implicitly return the result of the expression. This reduces boilerplate and makes the intent of the code immediately obvious. By adopting these modern patterns, you ensure your plugin remains compatible with current PHP versions and adheres to best practices for maintainability and performance.

Our network