This seems kind of limitation to me, indeed, if you use __toString() magic method for anything other than simple object variables concatenation, your code should be able to throw exceptions. Consider you have an object that generates XML as output, and you decide to provide even nicer interface, so that anyone using it in string context to get access to that XML. XML generation might not go well, and the obvious way to let the client know about this is to throw an exception. However, this is not possible (most probably due to some internal architecture limitations – as I honestly see no reason why this ideologically wrong).
One (not quite pretty) way to still provide some feedback from __toString() is using trigger_error:
public function __toString()
{
try {
$output = $this->generateXml();
return $output;
} catch(Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
return '';
}
}
If you know of a better option, let me know!
