# ArrayAccess and array_key_exists function

- Thread: https://externals.io/message/122435
- Original archive: https://news-web.php.net/php.internals/122435
- Started by ondrej@mirtes.cz on 2024-02-19T12:32:59+00:00
- Messages: 4
- RFC: Improve language coherence for the behaviour of offsets and containers (Inactive) — https://externals.io/rfc/container-offset-behaviour.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #122435 — ondrej@mirtes.cz — 2024-02-19T12:32:59+00:00

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

Hello everyone,
I was recently surprised that ArrayAccess does not work
with `array_key_exists()` function.

I am aware that ArrayAccess objects cannot be made to work with many array
functions because it does not provide a way to iterate over its keys, you
can only ask for and set specific previously known keys.

But it could be made to work with `array_key_exists()`, because it already
works with isset() and there’s a direct method that could be used for this
- offsetExists().

The use case I have in mind is that sometimes libraries that originally use
“array” in return types want to switch to objects and they make these
objects to implement ArrayAccess, for maximising the backward compatibility
with the old version. This mostly works, except for `array_key_exists()`.

Do you think we could add support for ArrayAccess objects in the 2nd
parameter of `array_key_exists()` function?

Thank you.

Ondřej Mirtes

---

## #122436 — Gina P. Banyard — 2024-02-19T13:21:19+00:00

URL: https://externals.io/message/122435#122436 · In reply to: #122435

> Hello everyone,
> I was recently surprised that ArrayAccess does not work with `array_key_exists()` function.
>
> I am aware that ArrayAccess objects cannot be made to work with many array functions because it does not provide a way to iterate over its keys, you can only ask for and set specific previously known keys.
>
> But it could be made to work with `array_key_exists()`, because it already works with isset() and there’s a direct method that could be used for this - offsetExists().
>
> The use case I have in mind is that sometimes libraries that originally use “array” in return types want to switch to objects and they make these objects to implement ArrayAccess, for maximising the backward compatibility with the old version. This mostly works, except for `array_key_exists()`.
>
> Do you think we could add support for ArrayAccess objects in the 2nd parameter of `array_key_exists()` function?
>
> Thank you.
>
> Ondřej Mirtes

Hello Ondřej,

I'm been working on overhaulling the semantics of "container" objects as ArrayAccess can have some peculiar behaviour, but this is because the underlying engine handlers are also complex.

I think this change makes sense and will keep it in mind/add it to my RFC.

Best regards,
Gina P. Banyard

---

## #122442 — Robert Landers — 2024-02-20T13:21:57+00:00

URL: https://externals.io/message/122435#122442 · In reply to: #122435

>
> Hello everyone,
> I was recently surprised that ArrayAccess does not work with `array_key_exists()` function.
>
> I am aware that ArrayAccess objects cannot be made to work with many array functions because it does not provide a way to iterate over its keys, you can only ask for and set specific previously known keys.
>
> But it could be made to work with `array_key_exists()`, because it already works with isset() and there’s a direct method that could be used for this - offsetExists().
>
> The use case I have in mind is that sometimes libraries that originally use “array” in return types want to switch to objects and they make these objects to implement ArrayAccess, for maximising the backward compatibility with the old version. This mostly works, except for `array_key_exists()`.
>
> Do you think we could add support for ArrayAccess objects in the 2nd parameter of `array_key_exists()` function?
>
> Thank you.
>
> Ondřej Mirtes

I'd love to see all array_* functions accept iterators/objects instead
of only just arrays. Yes, I know the name says "array" but often we
just mean "do this operation on this thing that looks enough like an
array for now."

Robert Landers
Software Engineer
Utrecht NL

---

## #122472 — timwolla@bastelstu.be — 2024-02-22T18:55:57+00:00

URL: https://externals.io/message/122435#122472 · In reply to: #122442

Hi

(Robert, Ondrej, please apologize the duplicate mail, the list rejected 
my first attempt, because the previously used alias tim@ is no longer 
accepted as a valid sender, I'll need to update my subscription)


> I'd love to see all array_* functions accept iterators/objects instead
> of only just arrays. Yes, I know the name says "array" but often we
> just mean "do this operation on this thing that looks enough like an
> array for now."
> 

This is probably a good opportunity to point out this mailing list 
thread once more:

https://externals.io/message/118896#118896

Best regards
Tim Düsterhus
