The indexes used in an ArrayAccess object are not limited to strings and integers as they are for arrays: you can use any type for the index as long as you write your implementation to handle them. This fact is exploited by the SplObjectStorage class.

Sadly you cannot assign by reference with the ArrayAccess (at least in PHP 5.3.23)It's too bad there is no syntax for optionally passing variables by reference to functions (a feature in retro PHP).That option would have let ArrayAccess fully mimic the functionality of normal array assignments:

Building on comments about incompatibility between plain arrays and ArrayAccess objects, many (most?) of the array_* methods won't work with ArrayAccess objects.Simple calls such as sizeof() work, but array_values() will throw an error, for instance.

You might be wondering whether implementing an ArrayAccess interface makes the class iterable. It is, after all, an "array". The answer is no, it doesn't. Additionally there are a couple of subtle gotchas if you add both and want it to be an associate array. The below is a class that has both ArrayAccess AND Iterator interfaces. And Countable as well just to be complete.

<?php//This uses return types which are only valid in PHP 7. They can be removed if you are forced to use an older version of PHP.//N.b. The offsetSet method contains a function that is only valid from PHP 7.3 onwards.

class HandyClass implements ArrayAccess, Iterator, Countable {

private $container = array(); //An Array of your actual values.private $keys = array(); //We use a separate array of keys rather than $this->position directly so that we canprivate $position; //have an associative array.

public function __construct() {$position = 0;

$this->container = array( //Arbitrary array for demo. You probably want to set this to empty in practice or"a" => 1, //get it from somewhere else, e.g. passing it into the constructor."b" => 2,"c" => 3, );$this->keys = array_keys($this->container); }

public function count() : int { //This is necessary for the Countable interface. It could as easily returnreturn count($this->keys); //count($this->container). The number of elements will be the same.}

public function offsetUnset($offset) { unset($this->container[$offset]); unset($this->keys[array_search($offset,$this->keys)]);$this->keys = array_values($this->keys); //This line re-indexes the array of container keys because if someone} //deletes the first element, the rewind to position 0 when iterating would //cause no element to be found.public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; }}?>