PrestaShop 9 errors
Call to undefined function each() when moving PrestaShop to PHP 8
PrestaShop 9 needs PHP 8.1 or newer, and PHP 8 removed each(), create_function() and other old functions. Modules written for PHP 5 or 7 stop with a fatal error.
- The error
Call to undefined function each()- Removed in
- PHP 8.0
- Usually found in
- Old modules, themes, overrides
What happened
PrestaShop 9 runs on PHP 8.1 to 8.4 (9.1 also on 8.5). A store coming from PrestaShop 1.7 often ran on PHP 7.x, so the upgrade changes PHP by a major version too. PHP 8.0 removed functions that had been deprecated for years. The most common ones in PrestaShop modules:
each(), used to walk through arrayscreate_function(), used to build small callbacksmoney_format(), used to format pricesget_magic_quotes_gpc(), used by old input filters
Each of them now ends with Call to undefined function ...(). PHP 8 is also stricter in other ways: for example, functions such as count() or strlen() given the wrong type throw an error instead of a warning, and implode() no longer accepts its arguments in reverse order.
The fix
The replacements are straightforward:
// each()
// Before
while (list($key, $value) = each($array)) { ... }
// After
foreach ($array as $key => $value) { ... }
// create_function()
// Before
usort($items, create_function('$a, $b', 'return $a["position"] - $b["position"];'));
// After
usort($items, function ($a, $b) { return $a['position'] - $b['position']; });
// money_format(): use the PrestaShop locale instead
$formatted = Context::getContext()->getCurrentLocale()->formatPrice($amount, $isoCode);
get_magic_quotes_gpc() always returned false in PHP 7.4, so the check around it can simply be removed, keeping the "false" branch.
Where else it hides
grep -rnw "each\|create_function\|money_format\|get_magic_quotes_gpc" --include=*.php modules/ themes/ override/
The search above finds the names; check each hit, because a method called each() on a collection object is fine. A full PHP 8 check is part of our free readiness check below, along with the PrestaShop changes.