Article

Replacing create_function()

Remove create_function() safely: lifecycle, modern replacements, and migration examples for legacy PHP apps.

create_function() built an anonymous function from string code at runtime. It was slow, hard to analyze statically, and a vector for injection when strings were concatenated from user input.

Lifecycle (php.net)

  • Deprecated: PHP 7.2.0
  • Removed: PHP 8.0.0

Modern replacement

Use real closures or first-class callables. Never eval user-controlled strings to recreate it.

Migration example

# LEGACY — create_function()
$mul = create_function('$a,$b', 'return $a * $b;');
echo $mul(3, 4);

# MODERN
$mul = static fn(int $a, int $b): int => $a * $b;
echo $mul(3, 4);

Scan before cutover

Search the tree for create_function() and paste samples into the PHP Version Compatibility Checker and PHP Deprecated Checker.

Related tools

Related reading