[php-src] Issue #7825: Add nullsafe call and index access
| From: | Vovan-VE | Date: | Sat, 25 Dec 2021 05:08:25 +0000 |
| Subject: | [php-src] Issue #7825: Add nullsafe call and index access | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238561@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7825
Author: Vovan-VE
### Description
### Proposal
```php
// Nullsafe call
$res = $optionalClosure?->(1, 2, 3);
$res = ($obj->optionalClosure)?->(1, 2, 3);
// or even?
$res = $obj->optionalClosure?->(1, 2, 3);
// Nullsafe index access
$res = $optionalArray?->[42];
$res = $obj->optionalArray?->[42];
```
### Backward Incompatible Changes
No.
### Motivation
1. Nullsafe access operator
?-> already exists and whole expression consists of
existing tokens only;
2. Syntax like $fn?() and $arr?[42] is not possible since it's a part
of already valid syntax ?: ;
3. Syntax like $fn?‗() and $arr?‗[42] (where ‗ is
anything else) most probably will never be added because ‗ is literally something
else and leads to inconsistency with $obj?->member.
### Open Issues
* Must $obj->optionalClosure require parentheses
($obj->optionalClosure)?->() like ($obj->closure)() does?
### Future Scope
Probably it may be a small step forward to $array->methods() like
[1,2,3]->map(fn ($x) => $x * 10) which itself may be a step to more features like
(-42)->abs(), $str->trim()->replace(...)->replace(...).