No, you misunderstood the ArrayAccess utility. It is not just a kind of wrapper for an array. Yes, a standard example of its implementation uses the private variable $array , the functionality of which ends with the class, but this is not particularly useful. Often you can just use an array.
One good example of ArrayAccess is when the script does not know which variables are available.
As a rather stupid example, imagine an object that worked with a remote server. Resources on this server can be read, updated, and deleted using the API over the network. The programmer decides that they want to wrap this functionality with an array-like syntax, so $foo['bar'] = 'foobar' sets the bar resource on this server so that foobar and echo $foo['bar'] retrieve it. The script has no way of knowing which keys or values ββare present without trying to use all possible values.
So, ArrayAccess allows you to use array syntax to install, update, retrieve, or remove from an object with an array type syntax: nothing more, no less.
Another Countable interface allows the use of count() . You can use both interfaces in one class. Ideally, there would be more such interfaces, possibly including those that can execute array_values or array_keys , but they currently do not exist.
source share