[PHP-BUG] Req #75017 [NEW]: Nullable Method Calling

[email protected] ("php at haravikk dot me") Tue, 1 Aug 2017 13:15:44 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             php at haravikk dot me
Operating system: 
PHP version:      Next Major Version
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Nullable Method Calling

Description:
------------
It was great to see the addition of the nil-coalescing operator in PHP
7, but one case that I encounter a lot but still appears to have no
solution is calling a method on a variable that may be null.

What I'd like to propose is a new nullable method calling operator,
which would enable us to call a method on a variable that may be null;
if the variable is null the method call is ignored and null is returned,
otherwise the method is called and its result returned. My proposed
operator for this would be `?->`, i.e- a normal method operator, but
with a question mark after the variable indicating that the developer is
aware it could be null. Put of another way, if `->` is "call method",
then `?->` "call method if not null".

We could then use this like so:

`$array = $object?->getFoo()?->getArray() ?? [];`

Here we use the operator to try to drill down into `$object`, seeking
out an array. If either `$object` or `$object->getFoo()` returns `null`,
then null is returned, triggering the nil coalescing operator and
resulting in a default empty array.

NOTE: This operator only handles `null` cases, i.e- if any of the
methods above are called upon an object that doesn't have them, then an
error is thrown as normal.


-- 
Edit bug report at https://bugs.php.net/bug.php?id=75017&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75017&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75017&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75017&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75017&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75017&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75017&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75017&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75017&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75017&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75017&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75017&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75017&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75017&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75017&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75017&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75017&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75017&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75017&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75017&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75017&r=mysqlcfg