Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ToStringTrait.php

function ToStringTrait::__toString

Implements the magic __toString() method.

File

core/lib/Drupal/Component/Utility/ToStringTrait.php, line 13

Class

ToStringTrait
Wraps __toString in a trait to avoid some fatal errors.

Namespace

Drupal\Component\Utility

Code

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 ' . static::class . ' object in ' . $e->getFile() . ' on line ' . $e->getLine() . ': ' . $e->getMessage(), E_USER_WARNING);
        // 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();
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal