Call to undefined function create_function() after moving to PHP 8
Fatal 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.
Fix it fast
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 .2Match what you see, then jump to its fix
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.phpConfirmed by php.net
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-dependencies3Verify
No match remains in vendor, and the app boots on the new PHP.
grep -rn "create_function" vendor/ | wc -lConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix