# Nullsafe array access can still emit a warning

- Thread: https://externals.io/message/118460
- Original archive: https://news-web.php.net/php.internals/118460
- Started by Gert de Pagter on 2022-08-24T12:58:45+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #118460 — Gert de Pagter — 2022-08-24T12:58:45+00:00

URL: https://externals.io/message/118460#118460

Hi Internals,

I recently learned that using null safe on an array access can still
emit a warning when the array access is not defined. I kinda expected
it to work like a null coalesce and just short circuit there,without
the warning.

Is there any chance this behaviour could be changed in a n upcoming
PHP version? I didn't see anything mentioned specifically about this
in the RFC.

https://3v4l.org/1raa8

Greetings,
Gert de Pagter/ BackEndTea

---

## #118461 — Marco Pivetta — 2022-08-24T13:11:06+00:00

URL: https://externals.io/message/118460#118461 · In reply to: #118460

Hey Gert,



> Hi Internals,
>
> I recently learned that using null safe on an array access can still
> emit a warning when the array access is not defined. I kinda expected
> it to work like a null coalesce and just short circuit there,without
> the warning.
>
> Is there any chance this behaviour could be changed in a n upcoming
> PHP version? I didn't see anything mentioned specifically about this
> in the RFC.
>
> https://3v4l.org/1raa8
>
> Greetings,
> Gert de Pagter/ BackEndTea
>

This seems expected behavior to me: it's "nullsafe", not "undefinedsafe".

If it operated like `??`, then `$undefined?->bar()` would be valid too,
which seems wrong, IMO.

Marco Pivetta

https://twitter.com/Ocramius

https://ocramius.github.io/

---

## #118462 — Gert de Pagter — 2022-08-24T13:17:31+00:00

URL: https://externals.io/message/118460#118462 · In reply to: #118461

`$undefined?->bar()` does feel wrong, but `$arr[0]?->bar()` does not to me.

But maybe i've just been using javascript too much lately


>
> Hey Gert,
>
> 
> > […]
>
>
> This seems expected behavior to me: it's "nullsafe", not "undefinedsafe".
>
> If it operated like `??`, then `$undefined?->bar()` would be valid too, which seems wrong, IMO.
>
> Marco Pivetta
>
> https://twitter.com/Ocramius
>
> https://ocramius.github.io/

---

## #118463 — Christoph M. Becker — 2022-08-24T14:16:05+00:00

URL: https://externals.io/message/118460#118463 · In reply to: #118460

> I recently learned that using null safe on an array access can still
> emit a warning when the array access is not defined. I kinda expected
> it to work like a null coalesce and just short circuit there,without
> the warning.
>
> Is there any chance this behaviour could be changed in a n upcoming
> PHP version? I didn't see anything mentioned specifically about this
> in the RFC.
>
> https://3v4l.org/1raa8

That behaves as documented[1]:

| The nullsafe operator works the same as property or method access as
| above, except that if the object being dereferenced is null then null
| will be returned rather than an exception thrown.

And particularly:

| The effect is similar to wrapping each access in an `is_null()` check
| first, but more compact.

However, see e.g.
<https://github.com/php/php-src/issues/7825#issuecomment-1100052164>.

[1]
<https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.nullsafe>

--
Christoph M. Becker

---

## #118464 — Eugene Sidelnyk — 2022-08-24T18:32:12+00:00

URL: https://externals.io/message/118460#118464 · In reply to: #118461

I completely agree with Marco. We should not mix up nullsafe operator with
array access. As for me, aforementioned code works as expected.

As an improvement, we can think of access-safe operator (like `$arr[?0]`),
however I'm not sure if introduction of such will make sense.

Regards
Yevhen



> Hey Gert,
>
> 
>
> > […]
>
> This seems expected behavior to me: it's "nullsafe", not "undefinedsafe".
>
> If it operated like `??`, then `$undefined?->bar()` would be valid too,
> which seems wrong, IMO.
>
> Marco Pivetta
>
> https://twitter.com/Ocramius
>
> https://ocramius.github.io/
