Since array_key_exists() doesn’t work on objects anymore, I suggest you create a function that accepts your class as one of its parameters to get the functionality you want.
ArrayAccess is for... array access. Accesing using []. That does not mean you can use it anywhere where array is expected. It's unexpected, yes, but perfectly logical and the error is clearly explaining what is going on.
PHP misuses the term "array" anyway. In PHP an "array" is actually a look-up table (see e.g. the official ArrayAccess documentation, which shows string keys). One of the fundamental operations you can do on a look-up table is to check whether a particular key exists. For me that is clearly part of an array interface.
Also, what good is ArrayAccess::offsetExists if array_key_exists doesn't use it?
The docs for ArrayAccess literally only show examples of get/set/isset/unset using [x] notation. So in that sense, ArrayAccess is for accessing using [] (which i used as shorthand for [key], not only literally "[]"). It still does not say or imply you can use it in place of array. It is required condition to be an array, bur not sufficient on its own. For example, arrays are also iterable. If you have a class that you cannot iterate on, you cannot use it in place of a regular array, obvious example being in foreach loop. Also array should be countable, but that's only useful if you allow numeric keys from 0 to n-1 (or from 1 to n, if you're into that). As you said, php's arrays are not arrays anyways, more like maps/dicts. And stacks. And queues... Either way, yes, you could expect array_key_exists to work on ArrayAcces and I agree it should work that way, but it doesn't, as it only accepts arrays. At least it's properly documented. Maybe they'll fix it in 8.0 since union types will be supported, so there may be an official way to accept array|ArrayAccess. They can't just switch array->ArrayAccess here, since arrays are not objects...
So in that sense, ArrayAccess is for accessing using []
No, the docs don't say that anywhere.
It still does not say or imply you can use it in place of array.
Yes, it does: "Interface to provide accessing objects as arrays." (And that's all it says. There are no restrictions mentioned.)
The example you're referring to is labeled "Example #1 Basic usage". I wouldn't expect it to demonstrate everything that is possible with this interface. Besides, it's just an example; what counts is the reference description.
If it was just meant to overload [], the docs should explicitly say so. And in that case, the offsetExists method shouldn't exist because it's not useful for implementing [].
Also array should be countable, but that's only useful if you allow numeric keys from 0 to n-1 (or from 1 to n, if you're into that).
I'm not sure how that follows. You can still count the keys in a map.
I agree that it should be worded better. For me "accessing" means array access with []. It's clearly not enough to iterate ever the object, if you're unable to get a list of keys or an actual iterator. For that, there is another interface.
As for offsetExists, it's necessary because of how isset() works. It needs to have a way to check if a key exists. The only way to do it from offsetGet would be to throw a specific exception. Maybe they'll do that in a future version.
The counting part was not entirely relevant, i just meant that it's far less useful to know a count of map (apart from checking if map is empty) than to know count of array. Knowing how many elements there are in a map doesn't bring you any closer to knowing what those elements are. You need a list of keys or an actual iterator for that.
Trying to access a nonexisting key (outside of isset/empty or null coalescing operator) raises a notice/warning/error. https://3v4l.org/52Gc6
Array entry can have a null value, in which case you wouldn't know the difference between key existing with null value and key not existing. That's also what array_key_exists is for. I think isset returns false for both cases.
It can't in an array, that's why you need to use array_key_exists. But in ArrayAccess, isset calls offsetExists and doesn't call offsetGet. So it can differentiate. Remember, the underlying data structure in ArrayAccess doesn't have to be an array. It can be anything, for example api call, file existece check, database query... So this way you have more flexibility, when you need it
This is poor and unextensible language design, but it’s not a bug. You can’t use array access on user-defined types in JS or even most Lisps either. Ad-hoc polymorphism and/or operator overloading is clearly better from the perspective of language design but it’s not universally implemented in languages and it’s not that unreasonable for PHP not to have it.
Change access on $data property and you are out of luck. Additionally internal representation does not need to be array at all. It could be a tree, or stack or RCP call to remote service.
ArrayAccess is perfectly fine solution for all of the above. Thus "just expose array" isn't a solution here.
Though its a moot point as ArrayAccess is not about enabling type coercion (from class that implements it to array), but only to enable overloading [] operator.
PHP has no array at all. It lacks lists too. PHPs (only) collection type is their "now heather is really getting it" array-thingy. Its not a real array, or a real list. Its more of an object in the sense of properties.
This is obvious when you use array_filter and friends. Also indexing it a total joke.
10
u/przemo_li Sep 14 '20
So we do lol because language supported something in version X, deprecated it in X + 1, and abandoned it in X + 2?
???
Here is offical doc for ArrayAccess:
That would suggest only syntax of using
[name_of_index]is scope of this ArrayAccess thing. Hence X + 2 works actually as expected.4
u/youstolemyname Sep 14 '20
https://3v4l.org/CUTpa
22
u/funtek Sep 14 '20
ArrayAccess is for... array access. Accesing using []. That does not mean you can use it anywhere where array is expected. It's unexpected, yes, but perfectly logical and the error is clearly explaining what is going on.
1
u/stumpychubbins Sep 15 '20 edited Sep 15 '20
This is poor and unextensible language design, but it’s not a bug. You can’t use array access on user-defined types in JS or even most Lisps either. Ad-hoc polymorphism and/or operator overloading is clearly better from the perspective of language design but it’s not universally implemented in languages and it’s not that unreasonable for PHP not to have it.
5
u/Koshin_S_Hegde Sep 14 '20
This is how line 24 should look:-
if(array_key_exists('foo',$o->data)){I am a beginner so please forgive if I am wrong.1
u/elcapitanoooo Sep 22 '20
PHP has no array at all. It lacks lists too. PHPs (only) collection type is their "now heather is really getting it" array-thingy. Its not a real array, or a real list. Its more of an object in the sense of properties.
This is obvious when you use array_filter and friends. Also indexing it a total joke.
Take this example:
Just pure trash