We have objects that implement __toString():
class Foo {
public function __toString()
{
return 'bar';
}
}
Then we have functions that return either string or object that implement __toString(), like the example above.
While using return type string for strings works of course:
function getString():string {
return 'Works!';
}
Returning object of type Foo does not work, because it's not a string:
function getString(Foo $foo):string {
return $foo; // Fatal TypeError!
}
Is there any PHP interface that we can use to type hint both string and objects that implement that unknown interface, e.g. Printable or Stringable (lol)?
The goal is to being able to return either string or objects that implement a specific interface (which would enforce the implementation of __toString).