Can’t use nullsafe operator in write context

Description

The nullsafe operator is only available for reading, not writing. Aka, it cannot be used on the left side of an assignation.

The nullsafe operator replaces any non-existing property with a null value: that value is not an object, and cannot handle properties on its own.

This limitation applies to assignations, short assignations, such as .=, +=, etc., pre and post incrementations ++$a->b, and references arguments.

Example

<?php

$x = new stdClass();

$x?->a = 1;

?>

Solutions

  • Use the -> operator for writing operations.