trait ToStringTrait in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/lib/Drupal/Component/Utility/ToStringTrait.php \Drupal\Component\Utility\ToStringTrait
Wraps __toString in a trait to avoid some fatals.
Hierarchy
- trait \Drupal\Component\Utility\ToStringTrait
2 files declare their use of ToStringTrait
- DateTimePlus.php in core/lib/ Drupal/ Component/ Datetime/ DateTimePlus.php 
- Contains \Drupal\Component\Datetime\DateTimePlus.
- TranslatableMarkup.php in core/lib/ Drupal/ Core/ StringTranslation/ TranslatableMarkup.php 
- Contains \Drupal\Core\StringTranslation\TranslatableMarkup.
File
- core/lib/ Drupal/ Component/ Utility/ ToStringTrait.php, line 13 
- Contains \Drupal\Component\Utility\ToStringTrait.
Namespace
Drupal\Component\UtilityView source
trait ToStringTrait {
  /**
   * Implements the magic __toString() method.
   */
  public function __toString() {
    try {
      return (string) $this
        ->render();
    } catch (\Exception $e) {
      // User errors in __toString() methods are considered fatal in the Drupal
      // error handler.
      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);
      // In case we are using another error handler that did not fatal on the
      // E_USER_ERROR, we terminate execution. However, for test purposes allow
      // a return value.
      return $this
        ->_die();
    }
  }
  /**
   * For test purposes, wrap die() in an overridable method.
   */
  protected function _die() {
    die;
  }
  /**
   * Renders the object as a string.
   *
   * @return string|object
   *   The rendered string or an object implementing __toString().
   */
  public abstract function render();
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| ToStringTrait:: | abstract public | function | Renders the object as a string. | 2 | 
| ToStringTrait:: | protected | function | For test purposes, wrap die() in an overridable method. | |
| ToStringTrait:: | public | function | Implements the magic __toString() method. | 
