3 namespace Drupal\Component\Utility;
6 * Wraps __toString in a trait to avoid some fatals.
11 * Implements the magic __toString() method.
13 public function __toString() {
15 return (string) $this->render();
17 catch (\Exception $e) {
18 // User errors in __toString() methods are considered fatal in the Drupal
20 trigger_error(get_class($e) . ' thrown while calling __toString on a ' . get_class($this) . ' object in ' . $e->getFile() . ' on line ' . $e->getLine() . ': ' . $e->getMessage(), E_USER_ERROR);
21 // In case we are using another error handler that did not fatal on the
22 // E_USER_ERROR, we terminate execution. However, for test purposes allow
29 * For test purposes, wrap die() in an overridable method.
31 protected function _die() {
36 * Renders the object as a string.
38 * @return string|object
39 * The rendered string or an object implementing __toString().
41 abstract public function render();