In recent PHP updates, they have added to various interfaces to allow the object to be processed as an array, such as ArrayAccess, Iterator, Countable, etc.
My question is whether it makes sense that the following should work:
function useArray(array $array) { print_r($array); } useArray(new ArrayObj(array(1,2,3,4,5));
As of now, PHP throws a hint type error because $array not technically an array. However, it implements all interfaces, which makes it basically identical to an array.
echo $array[0]; // 1 $array[0] = 2; echo $array[0]; // 2
Logically, an object should be able to be used wherever an array can be used, since it implements the same interface as the array.
Am I confused in my logic or does it make sense that if an object implements the same interface as an array, should it be accessible in all the same places?
source share