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