phpsmarty

Why will Smarty remove Support for using PHP Functions as Modifiers?


After switching to PHP 8.2 and Smarty 4.3, I get a lot of deprecation warnings like

PHP Deprecated: Using php-function "substr" as a modifier is deprecated and will be removed in a future release. Use Smarty::registerPlugin to explicitly register a custom modifier

This message comes for substr, rand, mt_rand, json_encode and others.

Why where the PHP Functions removed, and is there an easy way to get the function back from the Smarty Repository or does every user of Smarty has to write these functions on his own?

They where really useful ...


Solution

  • It can be solved in a very simple way, you just need to do this:

    $smarty->registerPlugin("modifier", "substr", "substr");
    $smarty->registerPlugin("modifier", "rand", "rand");
    $smarty->registerPlugin("modifier", "json_encode", "json_encode");
    

    And the same for every modifier you need. The other option is to replace the modifier with the use of the function, so this:

    {$var|substr:0:10}
    

    To this:

    {substr($var,0,10)}