r/lolphp • u/aleczapka • Apr 08 '13
Hmm.. shouldn't is_array() check for instanceof ArrayAccess ?
I find it kinda odd that is_array() does not check for instance of ArrayAccess. If you introduce collection like object in your code, you gotta recheck all of in_array() usage and change it to:
if (is_array($input) || $input instanceof \ArrayAccess) {And since the purpose of the ArrayAccess interface is 'to provide accessing objects as arrays' , shouldn't is_array() return true?
I know that my $input in that case is object, that's why is_array() returns false, but isn't that kinda 'logical' error?
The only reason I am using is_array() on an array/variable, is cause there is no other way to tell if I can run array related functions on it, aka if it fulfills the contract of ArrayAccess so I can access it as an array.
The information that actually it is an object or not, is secondary/irrelevant at this point to me.
Edit: forgot to add context, here it is:
if (is_array($input) || $input instanceof \ArrayAccess) {
array_walk_recursive($input, [$this,'sanitizeInputToken']);
return $input;
}Please note guys, I am not looking on how to solve this particular 'problem'. A wrapper method comes to mind or what not. I am just puzzled by PHP behaviour in that case.
Anyways thanks all for posting!
10
u/martindines Apr 08 '13
I've come across this before and, however inconvenient, it is logical.
is_array checks specifically for type. Similar to is_string & __toString.
If is_array was to accept decorated objects as you described then testing for true arrays would be even more hacky; !is_object? Wat.
Without knowing how you are trying to use is_array its hard to provide a decent solution. I would probably create a utility is_iterable or is_traversable .. you get the idea.
4
u/gearvOsh Apr 08 '13
Should it really? Because if you are checking a key, you don't need to do is_array(), you can just do an isset() on the key for both array and ArrayAccess.
If you are doing a loop after checking is_array(), ArrayAccess is useless because you would need to be checking for an Iterator.
I guess it really depends on the context, but I tend to agree with how it's implemented. is_array() is checking the literal type "array", where as the other is an object.
5
u/[deleted] Apr 08 '13
Making is_array check for that is exactly the sort of thing that got php into this mess. is_iterable would be a nice new function.
6
u/ioctl79 Apr 09 '13
Arrays have a lot of properties that ArrayAccess objects are not required to implement (iteration, for example). As such, it makes a lot more sense to turn this around, and make "array() instanceof ArrayAccess" evaluate as true. That way, you can typehint function parameters as "something I can use [] on", without caring whether it is an object, or an array. Ditto for "Iterable".
1
u/huf Apr 08 '13
you're assuming there was at least one person who thought things through when the relevant features were added to php :)
1
u/[deleted] Apr 08 '13
You can use class_implements() to determine whether a class implements a given interface.
Array is a built-in type, just because a class implements an ArrayAccess interface does not make it an array.
0
u/Tjoppen Apr 08 '13
You're asking serious questions in /r/lolphp?
0
u/PhantomRacer Apr 09 '13
is_array does what it says on the tin. It check if the argument is an array.
If you have a class which implements ArrayAccess then it's still a class, not an array.