rfc:nullsafe_calls

This is an old revision of the document!


PHP RFC: Nullsafe Calls

Introduction

This RFC proposes a new operator, the “nullsafe” operator ?->, which allows safe chaining of method calls. This is useful to easily propagate errors forward to the end of a computation which should fail if any of its sub-computations should fail.

Proposal

A very common pattern is to have some a computation consisting of a series of method calls, any one of which can return null. If any of them do return null, the entire computation should fail. Right now, writing something like this in PHP requires a bunch of boilerplate checks after each call. For example:

function f($o) {
  $o2 = $o->mayFail1();
  if ($o2 === null) {
    return null;
  }
 
  $o3 = $o2->mayFail2();
  if ($o3 === null) {
    return null;
  }
 
  $o4 = $o3->mayFail3();
  if ($o4 === null) {
    return null;
  }
 
  return $o4->mayFail4();
}

Backward Incompatible Changes

What breaks, and what is the justification for it?

Proposed PHP Version(s)

This is proposed for the next major version of PHP, currently PHP 7.

RFC Impact

To SAPIs

Describe the impact to CLI, Development web server, embedded PHP etc.

To Existing Extensions

Will existing extensions be affected?

To Opcache

It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.

Please explain how you have verified your RFC's compatibility with opcache.

New Constants

Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation.

php.ini Defaults

If there are any php.ini settings then list:

  • hardcoded default values
  • php.ini-development values
  • php.ini-production values

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

This RFC does not change any existing PHP behavior, including the -> operator, the ?? operator, or other error suppression mechanisms.

Future Scope

This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC.

Proposed Voting Choices

This is pretty clearly a core language change and so requires 2/3. The vote will be a straight yes/no vote on accepting the new operator.

Patches and Tests

I have a scratch branch at https://github.com/jwatzman/php-src/compare/nullsafe-scratch?expand=1 with a working implementation. As the RFC progresses, I plan to squash that down into a nicer PR for review.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/nullsafe_calls.1414019515.txt.gz · Last modified: 2017/09/22 13:28 (external edit)