Description

Since PHP 8.0, the stringable interface1 is automatically added to classes that implement the __toString() method

Syntax

class Foo
{
    public function __toString(): string
    {
        return 'foo';
    }
}
 
function bar(string|Stringable $stringable) { /* … */ }
 
bar(new Foo());
bar('abc');

Footnotes

Footnotes

  1. https://wiki.php.net/rfc/stringable