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;
                }
        }

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.

  1. Change the display method to
            public function display($tpl = null)
            {
                  if(!is_null($tpl))
                     $this->config['template'] = $tpl;
                    echo $this->__toString();
            }
  2. 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

Anonymous's picture

That is why you should never call magic methods directly. __toString() is not meant to be called directly.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <csharp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Twitter-style @usersnames are linked to their Twitter account pages.

More information about formatting options