PHP __toString() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::__toString() returns a string representation of the exception. It usually includes the class name, message, code, file, line, and stack trace.

PHP calls __toString() when you use the exception as a string, for example with echo $e or in string interpolation. Unlike most getters, subclasses may override __toString() to change the format.

Syntax

Call __toString() with the values you need. Start with this line.

PHP
echo $e;
CallWhat it does
(string) $eCasts the exception to a string and calls __toString().
echo $e;Prints the full exception string, including the stack trace.
$e->__toString()Calls the method directly; usually not needed.

__toString() can be overridden in a subclass. The default Exception version includes message details and the stack trace string.

Library declarations
PHP
public Exception::__toString(): string

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
stringA readable summary of the exception, typically including the stack trace.

PHP Examples

Example 1 — Echo an Exception

Catch an Exception and print it with echo, which calls __toString().

PHP
<?php
try {
    throw new Exception("Unable to send mail", 17);
} catch (Exception $e) {
    echo substr((string) $e, 0, 40), "...", PHP_EOL;
}

How It Works

  1. throw creates the Exception with a message and code.
  2. Casting to string calls __toString().
  3. The result starts with the class name and message.
  4. substr() keeps the demo output short.

Example 2 — Override __toString() in a Subclass

Create a custom exception that returns a short string without a stack trace.

PHP
<?php
class ShortException extends Exception
{
    public function __toString(): string
    {
        return __CLASS__ . ": [" . $this->getCode() . "] " . $this->getMessage();
    }
}

try {
    throw new ShortException("Blocked", 403);
} catch (ShortException $e) {
    echo $e, PHP_EOL;
}

How It Works

  1. ShortException overrides __toString().
  2. The custom format uses getCode() and getMessage().
  3. echo $e uses the override automatically.
  4. getMessage() itself stays final and unchanged.

Example 3 — Compare __toString() and getMessage()

Show that getMessage() is only the text, while __toString() is a fuller report.

PHP
<?php
$e = new Exception("Only the text", 7);
echo "Message: ", $e->getMessage(), PHP_EOL;
echo "Starts: ", substr($e->__toString(), 0, 24), PHP_EOL;

How It Works

  1. getMessage() returns Only the text.
  2. __toString() begins with Exception: Only the text.
  3. The full __toString() value continues with file, line, and stack details.
  4. Choose getMessage() for UI text and __toString() for developer logs.

Frequently Asked Questions

Whenever PHP needs the exception as a string, such as echo $e, "Error: $e", or an explicit (string) cast.
Yes. Unlike getMessage() and most other Exception methods, __toString() is not final.
The default Exception implementation does. A subclass may return a shorter string.
Usually no. Casting or echoing the object is clearer and still calls the method.
getTraceAsString() is only the stack. __toString() adds class, message, code, and location as well.

Next: __clone()

Exception::__clone() exists to stop you from cloning exception objects. The method is fina.

__clone() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful