Entertainment at it's peak. The news is by your side.

PHP RFC: Nullsafe Operator

  • Date: 2020-06-02

  • Creator: Ilija Tovilo,

  • Assign: Applied

  • Purpose Version: PHP 8.0


This RFC proposes the fresh nullsafe operator ?-> with fat short circuiting.


It’s somewhat frequent to fully desire to call a intention or get a property on the outcomes of an expression if it’s no longer null.

At fresh in PHP, checking for null leads to deeper nesting and repetition:

$nation =  null;
if ($session !== null) {
    $particular person = $session->particular person;
    if ($particular person !== null) {
        $tackle = $particular person->getAddress();
        if ($tackle !== null) {
            $nation = $tackle->nation;
// carry out something with $nation

With the nullsafe operator ?-> this code can also as a replace be written as:

$nation = $session?->particular person?->getAddress()?->nation;
// carry out something with $nation

When the left hand aspect of the operator evaluates to null the execution of all of the chain will pause and evalute to null. When it’s no longer null this may occasionally behave precisely cherish the frequent -> operator.

Short circuiting


Short circuiting refers to skipping the review of an expression per some given situation. Two frequent examples are the operators && and ||. There are three methods the nullsafe operator ?-> can also put in pressure short circuiting. We’ll detect at the the same code snippet for every possibility.


1. Short circuiting for neither intention arguments nor chained intention calls

This entire lack of short circuiting is presently thoroughly fresh in Hack. Each and each the feature bar() and the diagram baz() are called. baz() will cause a “Call to a member feature on null” error. Evaluating intention arguments makes it essentially the most handsome of the three alternate choices. This used to be the major criticism of the closing RFC.

2. Short circuiting for intention arguments however no longer chained intention calls

This is what would on the entire be judicious lack of short circuiting. The feature bar() is no longer called, the diagram baz() is. baz() will cause a “Call to a member feature on null” error.

3. Short circuiting for every intention arguments and chained intention calls

We’ll focus on with this as fat short circuiting. Neither the feature bar() nor the diagram baz() are called. There will seemingly be no “Call to a member feature on null” error.


This RFC proposes fat short circuiting. When the review of 1 ingredient in the chain fails the execution of all of the chain is aborted and all of the chain evaluates to null. The following system are judicious piece of the chain.

  • Array salvage admission to ([])

  • Property salvage admission to (->)

  • Nullsafe property salvage admission to (?->)

  • Static property salvage admission to (:: )

  • Technique call (->)

  • Nullsafe intention call (?->)

  • Static intention call (:: )

The following system will cause fresh sub-chains.

  • Arguments in a feature call

  • The expression in [] of an array salvage admission to

  • The expression in {} when gaining access to properties (->{})

Chains are robotically inferred. Most productive the closest chain will end. The following examples will try for instance.

   $foo = $a?->b();
// --------------- chain 1
//        -------- chain 2
// If $a is null chain 2 is aborted, intention b() is no longer always called, null is assigned to $foo
// --------------- chain 1
//        -------  chain 2
// If $a is null chain 1 is aborted, intention b() is no longer always called, the expression `$c->d()` is no longer evaluated
// --------------- chain 1
//       --------  chain 2
// If $c is null chain 2 is aborted, intention d() is no longer always called, null is passed to `$a->b()`


1. It avoids surprises

$foo = null;

The review of expensive_function() is undesirable if $foo is null as its end result will simply be discarded. If the feature has aspect outcomes it can well additionally lead to surprises.

2. That chances are high you’ll also survey which methods/properties return null

$foo = null;

Without short circuiting every subsequent intention call and property salvage admission to in the chain will require utilizing the nullsafe operator otherwise you doubtlessly can salvage a “Call to a member feature on null” error. With short circuiting this isn’t important which makes it more glaring which methods/properties can also return null.

3. Mixing with other operators

$foo = null;
$baz = $foo?->bar()['baz'];
// Without short circuiting: 
// Recognize: Looking out to salvage admission to array offset on cost of form null
// With short circuiting

Since with short circuiting the array salvage admission to ['baz'] will seemingly be thoroughly skipped no peep is emitted.

Other languages

Lets detect essentially the most neatly-preferred high-stage programming languages (in accordance with the Stack Overflow 2020 see) and our sister language Hack to detect how the nullsafe operator is utilized.

In Object-C gaining access to properties and calling methods on nil is continually omitted

† Imaginable by diagram of DSL

‡ Hack evaluates intention arguments even though the left hand aspect of ?-> is null

8/13 languages have faith a nullsafe operator. 4/8 of these put in pressure the nullsafe operator with short circuiting.

Syntax alternative

The ? in ?-> denotes the exact feature in the code the build the short circuiting happens. It closely resembles the syntax of 1 any other language that implements a nullsafe operator.

Forbidden usages

Nullsafe operator in write context

Using the nullsafe operator in write context ist no longer allowed.

$foo?->bar->baz = 'baz';
// Can not utilize nullsafe operator in write context
foreach ([1, 2, 3] as $foo?->bar->baz) {}
// Can not utilize nullsafe operator in write context
// Can not utilize nullsafe operator in write context
[$foo?->bar->baz] = 'baz';
// Assignments can thoroughly happen to writable values

It used to be beforehand urged to permit the nullsafe operator in the left hand aspect of assignments and skip the project if the left hand aspect of the nullsafe operator used to be null. On the alternative hand, resulting from technical difficulties here is no longer a component of this RFC. It’ll be addressed in a later RFC. It is also no longer thoroughly sure whether the upright hand aspect of the project can also serene continually be evaluated or no longer.


Taking the reference of a nullsafe chain is no longer allowed. It is because references require l-values (memory areas, cherish variables or properties) however the nullsafe operator can on occasion return the r-cost null.

$x = &$foo?->bar;
// Might presumably loosely be translated to
if ($foo !== null) {
    $x = &$foo->bar;
} else {
    $x = &null;
    // Most productive variables wants to be assigned by reference

For this cause, the following examples are disallowed.

// 1
$x = &$foo?->bar;
// Compiler error: Can not derive reference of a nullsafe chain
// 2
// Error: Can not streak parameter 1 by reference
// 3
feature &return_by_ref($foo) {
    return $foo?->bar;
    // Compiler error: Can not derive reference of a nullsafe chain

Instance 2 is a runtime error because we are in a position to’t know at assemble time if the given parameter permits passing values by reference.

Backward Incompatible Changes

There are no longer any identified backward incompatible adjustments in this RFC.

Future Scope

Since PHP 7.4 a peep is emitted on array salvage admission to on null (null["foo"]). Thus the operator ?[] may perchance well additionally be necessary ($foo?["foo"]). Unfortunately, this code introduces a parser ambiguity resulting from the ternary operator and short array syntax ($foo?["foo"]: ["bar"]). As a result of this complication the ?[] operator is no longer piece of this RFC.

A nullsafe feature call syntax ($callableOrNull?()) is also outside of scope for this RFC.


Balloting begins 2020-07-17 and ends 2020-07-31.

Read More

Leave A Reply

Your email address will not be published.