PHP 8 News: Nullsafe Operator

Antonio Silva - Feb 10 - - Dev Community

The Nullsafe Operator, introduced in PHP 8.0, is a feature that simplifies the handling of null values in chains of method or property calls.

Nullsafe Operator (?->)

Purpose:

The Nullsafe Operator is designed to streamline the process of working with nullable values, particularly in scenarios where you have a chain of method or property calls, and any one of them might return a null value.

Syntax:

The syntax of the Nullsafe Operator is as follows:

$variable?->propertyOrMethod()
Enter fullscreen mode Exit fullscreen mode

Here, $variable is the variable that may be null, and propertyOrMethod is the property or method you want to access if $variable is not null.

Example:

Consider the following example without the nullsafe operator:

if ($object !== null) {
    $result = $object->getProperty();
    if ($result !== null) {
        $finalResult = $result->doSomething();
    }
}
Enter fullscreen mode Exit fullscreen mode

With the nullsafe operator, this can be simplified:

$finalResult = $object?->getProperty()?->doSomething();
Enter fullscreen mode Exit fullscreen mode

How it Works:

  • If $object is not null, the property access or method call proceeds.

  • If at any point in the chain a null value is encountered, the entire expression evaluates to null, and further method or property calls are skipped.

Chaining Methods and Properties:

You can chain multiple method or property calls using the nullsafe operator, making the code more concise:

$value = $object?->getProperty()?->method1()?->method2()->property;
Enter fullscreen mode Exit fullscreen mode

In this example, if any part of the chain returns null, the entire expression will be null.

Error Suppression:

It's important to note that the nullsafe operator does not suppress errors. If a method or property does not exist, a fatal error will still be triggered. However, if a null value is encountered, the expression gracefully evaluates to null without causing an error.

Short-Circuiting

The concept of short-circuiting in the context of the Nullsafe Operator in PHP refers to the early interruption of the execution of a chain of method or property calls as soon as a null value is encountered. This means that if a point in the chain returns a null value, subsequent calls are automatically skipped, and the result of the expression is immediately set to null.

Suppose you have a chain of method or property calls using the Nullsafe Operator, as in the following example:

$result = $object?->getProperty()?->method1()?->method2()->anotherProperty;
Enter fullscreen mode Exit fullscreen mode

In this example, if at any point in the chain a null value is encountered:

  1. $object is null: Execution stops there, and $result is immediately set to null. None of the subsequent calls (getProperty(), method1(), method2(), anotherProperty) are evaluated.

  2. getProperty() returns null: Execution stops, and $result is set to null. Subsequent calls (method1(), method2(), anotherProperty) are not evaluated.

  3. method1() returns null: Execution stops, and $result is set to null. Subsequent calls (method2(), anotherProperty) are not evaluated.

  4. method2() returns null: Execution stops, and $result is set to null. The subsequent call (anotherProperty) is not evaluated.

  5. anotherProperty is null: Execution stops, and $result is set to null.

This behavior is known as short-circuiting because as soon as a null value is encountered at any point in the expression, there is no need to evaluate the remaining method or property calls since the final result will be null anyway. This can make the code more efficient and concise, especially in situations where you need to avoid unnecessary evaluations in chains of methods or properties.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .