I am migrating my old PHP 5.2.14 and Smarty 2.6.19 to PHP 7.3.25 + Smarty 3.1.34.
The PHP code base is quite huge and I have used Smarty::_tpl_vars array to access assigned variables.
$smarty->assign("myvar","var-value");
$myvar = $smarty->_tpl_vars['myvar']; #this returns "var-value"
This method seem to have been deprecated and replaced with getTemplateVars
Is there any workaround for having _tpl_vars in the newer version of Smarty
2
Answers
Managed a workaround by adding the following snippet in Smarty.class.php of PHP 5.2/Smarty 2
I will replace all $smarty->_tpl_vars[] with $smarty->getTemplateVars() and this way ensure backward compatibility.
You could create a polyfill class that wraps the
getTemplateVars
method by implementing theArrayAccess
interface and assign it to the Smarty instance as_tpl_vars
. You would want to test this very carefully in your application. This is a proof of concept that I just whipped out and tested for this answer to demonstrate that it’s possible.In particular you should check to see if the Smarty instance needs to be passed into the constructor by reference in your PHP 5.2 code – I only have 7.4 handy to test with right now.