PHP Dictionary class accepting objects as keys I started creating a Dictionary class that accepts objects as there keys:

class Dictionary
    implements ArrayAccess
{    
    private $_keys = array();

    private $_values = array();

    public function offsetExists( $key )
    {
        return false !== array_search( $key, $this-&gt;_keys, true );
    }

    public function offsetGet( $key )
    {
        if( false === ( $index = array_search( $key, $this-&gt;_keys, true ) ) )
        {
            throw new OutOfBoundsException( 'Invalid dictionary key' );
        }

        if( !isset( $this-&gt;_values[ $index ] ) )
        {
            throw new LogicException( 'No matching value found for dictionary key' );
        }

        return $this-&gt;_values[ $index ];
    }

    public function offsetSet( $key, $value )
    {
        if( false !== ( $index = array_search( $key, $this-&gt;_keys, true ) ) )
        {
            $this-&gt;_values[ $index ] = $value;
        }
        else
        {
            $this-&gt;_keys[] = $key;
            $this-&gt;_values[] = $value;
        }
    }

    public function offsetUnset( $key )
    {
        if( false === ( $index = array_search( $key, $this-&gt;_keys, true ) ) )
        {
            throw new OutOfBoundsException( 'Invalid dictionary key' );
        }

        if( !isset( $this-&gt;_values[ $index ] ) )
        {
            throw new LogicException( 'No matching value found for dictionary key' );
        }

        array_splice( $this-&gt;_keys, $index, 1 );
        array_splice( $this-&gt;_values, $index, 1 );
    }
}


It works pretty well for the very elementary test cases I've done so far. However, I'm worried about three things:


Can you think of a situation where $this-&gt;_keys and $this-&gt;_values will not be synchronized with each other anymore, thus leading to data corruption? I thought maybe there could occur a situation where something might happen between the two array_splices, for instance, in:

public function offsetUnset( $key )
{
    ...

    array_splice( $this-&gt;_keys, $index, 1 );
    array_splice( $this-&gt;_values, $index, 1 );


... leading to data corruption.
I feel array_search() might be inefficient, but I specifically don't want to resort to using spl_object_hash() as keys (and thereby circumventing the need for an internal keys and values array) as I belief this is not bulletproof, as it doesn't guarantee hash uniqueness for objects at all.
I feel array_splice() might be inefficient, but I don't want to resort to unset() as I feel this could lead to unwieldy internal arrays keys eventually. I'd like to keep the internal arrays keys as tightly packed as possible.


Looking forward to your feedback.
First, I do have to say that you look like you are re-inventing the wheel. You can get all of that with SplObjectStorage. Since you're using OutOfBoundsException, you're already using the Spl types.

To your specific questions:


It is hard to think of such a situation, but that doesn't mean that it is inconceivable.
While spl_object_hash is not guaranteed unique, it is highly likely that you will run out of memory far before encountering a hash collision. As the documents state: A string that is unique for each currently existing object and is always the same for each object. It is both faster and cheaper than keeping/maintaining your own internal state.
Yes, the internal keys will not be quite as ordered in the internal arrays, but it will only make a substantial difference if you actually have a problem keeping the two arrays synced &mdash; basically, if you can get #1 to work consistently and without fail, then unset will not matter here.