Smarty, Shopware 5 and PHP 8

When Shopware 5 was created, obviously PHP 8 was far away in the future and no one would have been able to foresee the details of its impact on Smarty. The nice thing with Smarty is that it allows you to use PHP functions directly. For example, to check for the number of elements of an array you could use something like this (‘modifier notation’):

You could have even written this in a PHP function style as

Now obviously a PHP update of your server or your development environment directly reflects in your Smarty templates.

A typical pitfall is that many PHP functions have, in effect, been kind of null safe up to 7.4. That is, given $myArray === null, the function would just have issued a warning. This in turn would most likely have gone unnoticed in your Smarty template. What you’ll see with PHP 8 instead is something like

In case of the count function it seems to be sufficient to omit the PHP function call notation and to use the straight forward Smarty format, i.e. {$myArray|count}. How comes? The answer is simply that when using the PHP function style, the PHP function would be called directly. Whilst when using the modifier notation, Smarty will call a modifier function smarty_modifier_count provided with Shopware 5 (!), and, surprise, surprise, this will check the variable for being an instance of Countable and if not so, just return 0.

Now what works fine for count doesn’t neccessarily work for other PHP functions, as most of them do not have dedicated Smarty modifiers. There are functions like number_format that behave slightly different in PHP 8, and these differences sometimes are not explicitly documented.

View Post