PHP 5.3 __toString breaks Savant3 __toString with params
Just this week after upgrading my PHP version, I started getting this error when trying to use Savant 3: Fatal error: Method Savant3::__tostring() cannot take arguments in Savant3.php on line 192
PHP 5.3 no longer accepts parameters to it's magic function __toString() which conflicted with Savant's implementation which takes one parameter: $tpl.
public function __toString($tpl = null)
{
$output = $this->fetch($tpl);
if ($this->isError($output)) {
$text = $this->__config['error_text'];
return $this->escape($text);
} else {
return $output;
}
}
{
$output = $this->fetch($tpl);
if ($this->isError($output)) {
$text = $this->__config['error_text'];
return $this->escape($text);
} else {
return $output;
}
}
The fix involves changing this method so it doesn't take an argument and conforms with PHP 5.3. After a quick look at the code I realized the following changes were needed.
- Change the display method to
public function display($tpl = null)
{
if(!is_null($tpl))
$this->config['template'] = $tpl;
echo $this->__toString();
} - Then change the __toString method to use the config['template'] setting:
public function __toString()
{
$tpl = $this->config['template'];
$output = $this->fetch($tpl);
if ($this->isError($output)) {
$text = $this->__config['error_text'];
return $this->escape($text);
} else {
return $output;
}
}
Comments
That is why you should never call magic methods directly. __toString() is not meant to be called directly.
Post new comment