Are there any best practices for utilizing the ArrayAccess interface in PHP classes like Stackable or Threaded?

When utilizing the ArrayAccess interface in PHP classes like Stackable or Threaded, it is important to implement the required methods such as offsetExists, offsetGet, offsetSet, and offsetUnset to allow objects of the class to be accessed like arrays. It is also recommended to handle edge cases, such as checking for the existence of keys before accessing or modifying them.

class CustomArray implements ArrayAccess {
    private $container = [];

    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }

    public function offsetGet($offset) {
        return $this->container[$offset] ?? null;
    }

    public function offsetSet($offset, $value) {
        if ($offset === null) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }
}

// Example usage
$array = new CustomArray();
$array['key1'] = 'value1';
$array['key2'] = 'value2';

echo $array['key1']; // Output: value1

unset($array['key2']);
var_dump(isset($array['key2'])); // Output: bool(false)