Because there is no safe way to handle an exception thrown during __toString the easiest workaround for this is to catch all exceptions thrown and return either an empty string or null.

Since any invocation of __toString will expect a string back, it is a graceful way to handle any under-the-hood errors if you’re using __toString as a shortcut to echo out objects.

  • By: Andrew Kirkpatrick
  • In: PHP
  • Under: