rfc:throw_expression
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:throw_expression [2020/03/21 15:16] – created ilijatovilo | rfc:throw_expression [2020/04/23 15:30] (current) – Move to implemented ilijatovilo | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: throw expression ====== | ====== PHP RFC: throw expression ====== | ||
- | * Version: 0.9 | ||
* Date: 2020-03-21 | * Date: 2020-03-21 | ||
- | * Author: Ilija Tovilo, | + | * Author: Ilija Tovilo, tovilo.ilija@gmail.com |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
- | * Status: | + | * Status: |
* Implementation: | * Implementation: | ||
Line 14: | Line 13: | ||
<code php> | <code php> | ||
- | $callable = fn() => throw new Exception(); | ||
// This was previously not possible since arrow functions only accept a single expression while throw was a statement. | // This was previously not possible since arrow functions only accept a single expression while throw was a statement. | ||
+ | $callable = fn() => throw new Exception(); | ||
+ | // $value is non-nullable. | ||
$value = $nullableValue ?? throw new InvalidArgumentException(); | $value = $nullableValue ?? throw new InvalidArgumentException(); | ||
- | // $value can now be considered non-nullable. | ||
+ | // $value is truthy. | ||
$value = $falsableValue ?: throw new InvalidArgumentException(); | $value = $falsableValue ?: throw new InvalidArgumentException(); | ||
- | // $value | + | |
+ | // $value | ||
+ | $value = !empty($array) | ||
+ | ? reset($array) | ||
+ | : throw new InvalidArgumentException(); | ||
</ | </ | ||
Line 27: | Line 31: | ||
<code php> | <code php> | ||
+ | // An if statement could make the intention clearer | ||
$condition && throw new Exception(); | $condition && throw new Exception(); | ||
$condition || throw new Exception(); | $condition || throw new Exception(); | ||
$condition and throw new Exception(); | $condition and throw new Exception(); | ||
$condition or throw new Exception(); | $condition or throw new Exception(); | ||
- | // An if statement could make the intention clearer | ||
</ | </ | ||
===== Operator precedence ===== | ===== Operator precedence ===== | ||
- | If '' | + | If '' |
<code php> | <code php> | ||
- | try { | + | throw $this-> |
- | | + | // Evaluated as |
- | } catch (Exception | + | throw ($this->createNotFoundException()); |
- | echo $exception->getMessage(); | + | // Instead of |
- | //> | + | (throw $this)->createNotFoundException(); |
- | try { | + | throw static:: |
- | $exception = null; | + | // Evaluated as |
- | | + | throw (static:: |
- | } catch (Exception $e) {} | + | // Instead of |
- | echo $exception-> | + | (throw static):: |
- | //> Still pretty useless | + | |
- | try { | + | throw $userIsAuthorized |
- | $exception = null; | + | // Evaluated as |
- | | + | throw ($userIsAuthorized ? new ForbiddenException() : new UnauthorizedException()); |
- | } catch (Exception | + | // Instead |
- | echo $e-> | + | (throw $userIsAuthorized) ? new ForbiddenException() : new UnauthorizedException(); |
- | } | + | |
- | //> Kind of useful | + | |
- | </ | + | |
- | While these cases are probably more rare it's important they continue working. | + | throw $maybeNullException ?? new Exception(); |
+ | // Evaluated as | ||
+ | throw ($maybeNullException ?? new Exception()); | ||
+ | // Instead of | ||
+ | (throw $maybeNullException) ?? new Exception(); | ||
- | Although any binary operator can be used in the right-hand side of the '' | + | throw $exception = new Exception(); |
+ | // Evaluated as | ||
+ | throw ($exception = new Exception()); | ||
+ | // Instead | ||
+ | (throw $exception) = new Exception(); | ||
- | <code php> | + | throw $exception ??= new Exception(); |
- | throw new Exception() | + | // Evaluated as |
- | //> Notice: Object | + | throw ($exception ??= new Exception()); |
- | //> Fatal error: Uncaught Error: Can only throw objects in ... | + | // Instead |
+ | (throw $exception) ??= new Exception(); | ||
+ | |||
+ | throw $condition1 && $condition2 ? new Exception1() : new Exception2(); | ||
+ | // Evaluated as | ||
+ | throw ($condition1 && $condition2 ? new Exception1() | ||
+ | // Instead of | ||
+ | (throw $condition1) && $condition2 ? new Exception1() : new Exception2(); | ||
</ | </ | ||
- | This could change if the [[https:// | + | The common theme here is that everything after the '' |
- | For these reasons, this RFC proposes to use the lowest possible | + | The only downside of the low precedence is that a '' |
- | + | ||
- | One minor draw back is that this code would not be possible: | + | |
<code php> | <code php> | ||
Line 99: | Line 112: | ||
Proposed version is PHP 8. | Proposed version is PHP 8. | ||
- | ===== RFC Impact | + | ===== Voting |
- | ==== To SAPIs ==== | + | Voting starts 2020-04-05 and ends 2020-04-19. |
- | None. | + | |
- | ==== To Existing Extensions ==== | + | As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch. |
- | None. | + | |
- | ==== To Opcache ==== | + | <doodle title="Would you like to convert the throw statement into an expression?" |
- | None. | + | * Yes |
- | + | * No | |
- | ===== Proposed Voting Choices ===== | + | </doodle> |
- | As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch. | + | |
===== References ===== | ===== References ===== |
rfc/throw_expression.1584803813.txt.gz · Last modified: 2020/03/21 15:16 by ilijatovilo