Please wait

Null Safe Operator

The null safe operator, introduced in PHP 8.0, is a feature that helps you handle situations when you're trying to access properties or methods on an object that could potentially be null.

The problem

Imagine you have a chain of commands like this:

$user->getProfile()->getPhoneNumber()

Now, what if $user->getProfile() returns null because the user doesn't have a profile? Then trying to call getPhoneNumber() on null will result in an error.

To protect against this, traditionally, you might have to write additional condition checks to make sure each part of the chain isn't null. It would look something like this:

if ($user->getProfile() !== null) {
  $phoneNumber = $user->getProfile()->getPhoneNumber();
}

The solution

With the null safe operator ?->, PHP provides a shorter, cleaner way of doing this. By adding the ? before the ->, PHP will check for the value on the left for a null value.

If any part of the chain returns null, PHP will stop the rest of the chain and return null for the whole expression. Here's how you can use the null safe operator:

$phoneNumber = $user?->getProfile()?->getPhoneNumber();

In this case, if getProfile() returns null, PHP will not attempt to call getPhoneNumber(). Instead, it will assign null to $phoneNumber.

The null safe operator can make your code much cleaner and more readable when dealing with chains of method or property accesses where any part could potentially be null.

Read-Only

The null safe operator (?->) in PHP is read-only. This means you can use it to safely attempt to read a property or call a method on an object that might be null, but you cannot use it to try to set a property on an object that might be null.

For example, this is okay:

$result = $object?->method();

But trying to do this will result in a syntax error:

$object?->property = $value;

PHP will throw the following error message: Fatal error: Can't use null safe operator in write context

If you need to conditionally set a property on an object that might be null, you'll still need to use a traditional if statement to check if the object is not null before setting the property.

Evaluated left-to-right

The null-safe operator (?->) in PHP checks things from left to right. It doesn't jump ahead or give priority to other operations like function calls or array access.

Let's think about a shopping scenario. Suppose you have a customer who might have an address, and you want to set the country for the address based on some data from a GeoIP service.

Here's how the code might look:

$customer->getAddress()?->setCountry(getCountry());

Here's what's happening:

  1. $customer->getAddress()?- The ?-> operator checks if the customer has an address. If the customer doesn't have an address (getAddress() returns null), then it stops right there. getCountry() isn't called at all.
  2. If the customer does have an address, then getCountry() is called, and its result is used to set the country of the customer's address with setCountry().

Key takeaways

  • The null safe operator (?->) allows you to safely attempt to access properties or call methods on an object that could potentially be null.
  • If any part of the operation chain is null, PHP stops the rest of the chain and returns null for the whole expression.
  • The null safe operator is read-only. You cannot use it to try to set a property on an object that might be null.
  • The null safe operator is evaluated from left to right. If you need a different order of operations, you can use parentheses to group your operations.
  • This feature was added in PHP 8.0, so it's not available in older versions of PHP.

Comments

Please read this before commenting