__tostring()
Learn how PHP's __toString() magic method lets an object define its string representation, with runnable examples, common gotchas, and PHP's rules.
The __toString() Magic Method
__toString() is one of PHP's magic methods — special methods PHP calls automatically in certain situations. Specifically, PHP calls __toString() whenever an object is used where a string is expected: in echo, print, string concatenation with ., inside double-quoted strings, when passed to a function that type-hints string, and so on.
Without __toString(), trying to use an object as a string throws an error:
Object of class Foo could not be converted to stringBy defining __toString(), you decide what that string representation looks like. This page covers the method's signature, the rules PHP enforces, working examples, and the common gotchas.
Why use it
You reach for __toString() when an object has a meaningful textual form and you want it to "just work" in string contexts. Typical cases:
- A
Moneyobject that should render as"$19.99". - A
Userobject that should print as its full name in a template. - A value object (date, URL, coordinate) you want to log or echo without calling a getter every time.
It keeps calling code clean — echo $user; instead of echo $user->getFullName();.
Syntax
public function __toString(): stringThe method takes no arguments and must return a string. Since PHP 8.0 the : string return type is implicitly enforced even if you omit it — returning anything else triggers a TypeError.
Basic example
Here we give a Money object a readable string form:
<?php
class Money
{
public function __construct(
private int $cents,
private string $currency = 'USD'
) {}
public function __toString(): string
{
$amount = number_format($this->cents / 100, 2);
return "{$amount} {$this->currency}";
}
}
$price = new Money(1999);
echo $price; // 19.99 USD
echo "Total: {$price}"; // Total: 19.99 USDYou can run this with the try-it button above.
When echo $price runs, PHP sees an object where a string is needed, so it calls $price->__toString() behind the scenes and uses the returned value. The same thing happens inside the double-quoted string "Total: {$price}".
__toString() must return a string
The : string return type means PHP must end up with a string. Scalar values like 42 are coerced to "42" automatically, but values that cannot be coerced — such as an array — throw a TypeError:
<?php
class Broken
{
public function __toString(): string
{
return [1, 2]; // wrong: an array cannot become a string
}
}
echo new Broken();
// TypeError: Broken::__toString(): Return value must be of type string, array returnedAlways build and return a real string so the conversion is explicit and never surprises you.
Stringable interface
Since PHP 8.0, any class that declares __toString() automatically implements the built-in Stringable interface. You can type-hint Stringable (or string|Stringable) to accept "anything that can become a string":
<?php
function greet(string|Stringable $who): void
{
echo "Hello, {$who}!\n";
}
class Name implements Stringable
{
public function __construct(private string $value) {}
public function __toString(): string
{
return $this->value;
}
}
greet('world'); // Hello, world!
greet(new Name('Ada')); // Hello, Ada!Declaring implements Stringable explicitly is optional (PHP adds it for you) but makes your intent clear to readers and static analysis tools.
Gotchas
- You cannot
throwfrom__toString()before PHP 7.4. In PHP 7.4+ exceptions are allowed; on older versions throwing inside__toString()caused a fatal error. Keep the method simple and side-effect free. - It must be
public. A private or protected__toString()is not callable from the string context that triggers it. (string)casting also triggers it.(string) $objectis the explicit way to invoke__toString().
Conclusion
__toString() lets an object define its own string representation so it can be echoed, concatenated, and interpolated like a plain string. Keep it pure, always return a string, and consider type-hinting Stringable when a function should accept both strings and string-like objects. To go deeper, see PHP Classes and Objects, the constructor __construct() magic method, and PHP Strings.