Re: [RFC][DISCUSSION] throw expression
| From: | Andrea Faulds | Date: | Wed, 25 Mar 2020 19:04:17 +0000 |
| Subject: | Re: [RFC][DISCUSSION] throw expression | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109308@lists.php.net to get a copy of this message | ||
Hey Ilija,
Ilija Tovilo wrote:
Hi everybody! I hope you’re doing well. Due to the modest feedback I’d like to move the throw expression RFC to “under discussion”. https://wiki.php.net/rfc/throw_expression In short, the RFC proposes to convert the throw statement into an expression to make it usable in arrow functions, the coalesce operator, as well as the ternary/elvis operator.Thanks for this RFC, I have wanted this sometimes when writing PHP code. The ?? operator is one of those places, but I am surprised you haven't mentioned the
and and or operators.
It is classic PHP style (probably borrowed from Perl?) to write e.g.:
$mysql = mysql_open(...) or die("Couldn't connect to MySQL");
Obviously not very modern with use of mysql_ ;)
But the or operator is still quite handy for this kind of trivial error handling, and it would be great to be able to write e.g.:
$fp = @fopen($filename, "r") or throw new Exception("Couldn't open file!");
Likewise for and (I think it is less common):
$somethingWentWrong and throw new Exception("Oh no!");
So I would be happy to see throw become an expression for these reasons, even if it is a bit conceptually weird given it never returns anything!
Thanks,
Andrea