__toString Can Throw Exceptions¶
Until PHP 7.4, the magic method __toString() could not throw exception, in case of problem occurring during processing.
Since PHP 7.4, it is possible.
PHP code¶
<?php
class X {
function __toString() {
throw new \Exception('error'.__METHOD__);
}
}
(string) new X;
?>
Before¶
PHP Fatal error: Method X::__toString() must not throw an exception, caught Exception: errorX::__toString
Fatal error: Method X::__toString() must not throw an exception, caught Exception: errorX::__toString
After¶
PHP Fatal error: Uncaught Exception: errorX::__toString
Fatal error: Uncaught Exception: errorX::__toString
PHP version change¶
This behavior changed in 7.4