Stringable¶
stringable is a type, which represents data that may be used as a string: either a string, a heredoc or a string constant; or an object which has the __toString() method.
Stringable is automatically added to any class (and subclass) that has the __toString() method, though it is recommended to add it explicitly.
<?php
function foo(Stringable $s) {
print $s;
}
class MyString implements Stringable {
function __toString() {
return __METHOD__;
}
}
foo('foo'); // foo
foo(new X()); // MyString::__toString
?>
See also What is Stringable Interface in PHP 8?, PHP 8.0: New Stringable interface and The Stringable interface.
Related : String, PHP Native Interfaces, __toString() Method, PHP Natives
Added in PHP 8.0