wiki/content/20201110103409-php_stringable_interface.md

32 lines
517 B
Markdown
Raw Permalink Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-30 17:34:11 +00:00
date: 2020-11-10
2024-05-06 20:40:05 +00:00
id: f97470da-6833-4ecf-81e2-40dd04f6c2a1
title: PHP Stringable Interface
---
# Description
Since [PHP 8.0](20201109133834-php_8_0), the stringable interface[^1] is
automatically added to classes that implement the `__toString()` method
# Syntax
``` php
class Foo
{
public function __toString(): string
{
return 'foo';
}
}
function bar(string|Stringable $stringable) { /* … */ }
bar(new Foo());
bar('abc');
```
# Footnotes
[^1]: <https://wiki.php.net/rfc/stringable>