This is an old revision of the document!

PHP RFC: Nullsafe Calls


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.


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();

This certainly works, but it's a lot of boilerplate for a fairly common pattern, and it's also a lot of explicit checks that the runtime must do. Instead, a new operator is added to the language: ?->. Calling $obj?->foo(..) behaves identically to $obj->foo(..) if $obj is not null. If $obj is null, then it returns null.

This means that calls using this new operator can be chained together. For example, the code above, rewritten with the new operator:

function f($o) {
  return $o?->mayFail1()?->mayFail2()?->mayFail3()?->mayFail4();

Some not necessarily obvious implications of the above definition:

The ?-> operator is not valid to use for a member access, and will generate an E_COMPILE_ERROR if this is attempted. Defining such functionality is left to a further RFC.

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


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.


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


Rejected Features

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

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