[PHP-DEV] [RFC] [Discussion] PREG_THROW_ON_ERROR

Hi all,

Following the earlier pre-RFC discussion (https://news-web.php.net/php.internals/131783), I’ve written this up as a proper RFC: https://wiki.php.net/rfc/preg_throw_on_error

Short version:

a PREG_THROW_ON_ERROR flag you pass to any preg_*() call, so a PCRE error throws a \PregException you can catch
instead of a warning plus a false / null you have to notice and then chase through preg_last_error().
Same thing JSON_THROW_ON_ERROR and FILTER_THROW_ON_FAILURE already do for their functions.
It’s opt-in, so nothing changes without the flag.

The flag only changes how an error is delivered. A call does exactly the same thing with it or without it, byte for byte.
The only difference is that at the end, any error the call would have recorded is additionally thrown, carrying the same code and
message preg_last_error()/preg_last_error_msg() would report.

For array arguments that means the flag mirrors preg_last_error()…it throws whatever error the same call leaves there,
which for preg_replace() over an array is the last-processed entry, not the first. (In the pre-RFC I’d floated throwing on the first failing element instead).
Whether the preg_*() functions should stop at the first failing entry is a separate question about those functions, one that applies with or without the flag, so it’s out of scope here.

The exception name is settled from the pre-RFC as \PregException (thanks Tim) and the flag as PREG_THROW_ON_ERROR.

Target is the next feature release after 8.6 since 8.6’s beta1 is too close for this to conclude discussion and a vote in time.

I also saw Larry’s note asking to hold new business that isn’t targeting 8.6 until the freeze rush is over.
This won’t catch 8.6, so I’m not asking for any priority while the freeze is on. But the pre-RFC discussion was already open before
Larry’s note, so I’d rather keep it ticking over quietly than restart it in September.
If someone prefers I park it until then, please say so and I will.

Implementation and tests: https://github.com/php/php-src/pull/22797

Thanks,
Osama