Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Call to undefined function create_function() after moving to PHP 8

ErrorFatal error: Uncaught Error: Call to undefined function create_function()

Short answer

create_function() was deprecated in PHP 7.2.0 and removed in PHP 8.0.0, so on PHP 8 the call fails because the function no longer exists. Replace each call with a native anonymous function (closure). The manual gives the same swap for callbacks such as usort. Code on PHP 7.2 to 7.4 still runs but logs a deprecation.

  • Applies to PHP 7.4, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every call in your code and in vendor packages, and confirm which PHP version runs the site.

    php -v
    grep -rn "create_function" --include=*.php .
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace points into a file you wrote or a theme or plugin you maintainYour own code still calls create_function()Fix
    2. 2The failing file is under vendor/ or a plugin folder you did not writeThe call is inside a third-party package or an old pluginFix

What does the error look like?

  • Fatal error: Uncaught Error: Call to undefined function create_function()
  • Deprecated: Function create_function() is deprecated

Cause 1 of 2

Your own code still calls create_function()

1Check

The grep above lists each call. The first argument is a string of parameters, the second a string of code that was run through eval().

grep -rn "create_function" --include=*.php src/ app/ wp-content/

2Fix

Turn the two strings into a normal closure. The parameter string becomes the parameter list, the code string becomes the function body. The manual shows the same conversion for a usort callback.

// before
usort($list, create_function('$a,$b', 'return strlen($b) - strlen($a);'));

// after
usort($list, function ($a, $b) {
    return strlen($b) - strlen($a);
});

3Verify

Lint the file and run the page or script that failed.

php -l src/sort.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

The call is inside a third-party package or an old plugin

1Check

Look at which package owns the file and which release it is. The function is gone in PHP 8.0.0, so an old release written for PHP 7 cannot run there.

grep -rln "create_function" vendor/

2Fix

Update the package to a release that supports your PHP version. If none exists, replace the package, or edit its call sites into closures as above and stop shipping the old copy.

composer update vendor/package-name --with-dependencies

3Verify

No match remains in vendor, and the app boots on the new PHP.

grep -rn "create_function" vendor/ | wc -l

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run the code on PHP 7.4 with error_reporting set to E_ALL before an upgrade, so deprecations show up early.
  • Search the codebase for removed functions before changing the PHP version on the server.
  • Prefer closures for callbacks: they are checked at parse time, while create_function() built code at run time.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.