ArrayAccess and array_key_exists function

php.internals

Ondřej Mirtes

2 years ago
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

Gina P. Banyard

2 years ago
On Monday, 19 February 2024 at 12:32, Ondřej Mirtes <ondrej@mirtes.cz> wrote: > 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

Robert Landers

2 years ago
On Mon, Feb 19, 2024 at 1:35 PM Ondřej Mirtes <ondrej@mirtes.cz> wrote:
> > 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

Tim Düsterhus

2 years ago
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) On 2/20/24 14:21, Robert Landers wrote:
> 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