profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

PHP Stringable Interface

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');

Related

Footnotes