Fixed XF\Phrase::__toString() does not always coerce output to string


Well-known member
Affected version
If XF\Phrase::allowHtml is true, and $options['fallback'] = \XF::Phrase(....) and $options['fallbackRaw'] = true then renderPhrase will return a non-string causing XF\Phrase::__toString() to fail with an "Method XF\Phrase::__toString() must return a string value" error.

Note; $allowHtml = false, then escapeString will coerce the output to a string.

I think a simple solution is to add an explicit strval call or (string) cast;
public function __toString()
      return \strval($this->render());
Reproducing steps;

$phrase = \XF::phrase('bad_phrase');
$phrase->fallback(\XF::phrase('fallback_phrase'), false);
$phrase = (string)$phrase;

$phrase = \XF::phrase('bad_phrase');
$phrase->fallback(\XF::phrase('fallback_phrase'), true);
$phrase = (string)$phrase;
The 2nd case will fail
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.6).

Change log:
Ensure phrases are properly returned as strings
There may be a delay before changes are rolled out to the XenForo Community.
Top Bottom