After upgrading to PHP 8 I'm now seeing this Fatal error
what's wrong with my code ? And what is this Stringable
?
Fatal error: Declaration of Foo::__toString(): void must be compatible with Stringable::__toString(): string
1.2k views Asked by Rain At
2
Prior to PHP 8 it was possible for a programmer to write magic methods that have signatures that don't match the internal signature of them.
So for example
Although the return type
void
doesn't match the method's documentationPHP was not complaining!
However, with the advent of PHP 8 the signature of a magic method must follow that magic method's documentation.
Otherwise you'll see a
Fatal error
!Note: The
__construct()
and__destruct()
are excluded from this new change, since there is not concept of them returning values in almost every computer language.What is this Stringable?
Stringable
is an interface that is added automatically to classes that implement the__toString()
methodSee the RFC for more information.