Hi internals!

Currently it's not possible to throw an exception from __toString() -
instead you will get a (real) fatal error. This is becoming more and more
problematic as time goes on, e.g. with PHP 7 an exception can be triggered
as a result of a VM error or even a parse error.

I'd like to lift this restriction. A patch can be found here:
https://github.com/php/php-src/pull/1364

Apart from allowing exceptions, the patch also converts two leftover
recoverable fatal errors relating to __toString() into Error exceptions.

Furthermore the patch makes sure that we correctly (i.e. without leaks and
without superfluous notices or other side-effects) handle __toString()
exceptions in the engine. This includes concatenation and interpolation,
but also things like writing $foo->$object or ${$object}.
zend_parse_parameters() and Fast ZPP also deal with exceptions correctly.

However what it doesn't do, and what I wouldn't consider feasible to do, is
ensure that every single string conversion in library functions is
exception safe. Personally I don't think this is a blocking issue, as the
worst that can happen is usually an additional superfluous warning to be
thrown, or something similar. If cases like this turn up, we can
specifically target them.

It should also be noted that whatever issues with exceptions-safety may
remain, they already exist now (plus those the patch fixes), because the
two aforementioned recoverable fatal errors can be converted to exceptions
(and anyone doing blanket ErrorException conversions will do this).

So basically the question here is, is this partial solution acceptable for
us?

Thanks,
Nikita

Reply via email to