You could create a polyfill class that wraps the getTemplateVars
method by implementing the ArrayAccess
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.
<?php
class TplVarsPolyfill implements ArrayAccess
{
private $_smarty;
public function __construct(Smarty $smarty)
{
$this->_smarty = $smarty;
}
public function offsetSet($varName, $value)
{
$this->_smarty->assign($varName, $value);
}
public function offsetExists($offset)
{
$tplVars = $this->_smarty->getTemplateVars();
return isset($tplVars[$offset]);
}
public function offsetUnset($varName)
{
$this->_smarty->clearAssign($varName);
}
public function offsetGet($offset)
{
$tplVars = $this->_smarty->getTemplateVars();
return isset($tplVars[$offset]) ? $tplVars[$offset] : null;
}
/**
* This is required to convince Smarty to set the instance
* @see Smarty::__set
* @return mixed
*/
public function _tpl_vars()
{
return $this->_smarty->getTemplateVars();
}
}
$smarty = new Smarty();
$smarty->_tpl_vars = new TplVarsPolyfill($smarty);
$testVarName = 'foo';
$testVal = 'bar';
$smarty->assign($testVarName, $testVal);
assert($smarty->_tpl_vars[$testVarName]==$testVal, 'Value in polyfill should match what was assigned via assign()');
$polyfillVarName = 'animal';
$polyfillVal = 'wombat';
$smarty->_tpl_vars[$polyfillVarName] = $polyfillVal;
$smartyTemplateVars = $smarty->getTemplateVars();
assert($smartyTemplateVars[$polyfillVarName]==$polyfillVal, 'Value in getTemplateVars() should match what was assigned via polyfill');