I have an SplObjectStorage instance that stores element objects to be rendered in a container. I would like the ability to efficiently add and remove objects from any random position in the store.
Example:
<?php
$store = new SplObjectStorageWrapper;
$obj1 = new Obj;
$obj2 = new Obj;
$obj3 = new Obj;
$store->attach($obj1);
$store->attach($obj2);
$store->insertAtIndex($obj3, 1);
//Storage should now be organized as $obj1, $obj3, $obj2
How would I go about implementing the insertAtIndex method? Do I use a LimitIterator to detach and reattach children after a certain position? Using an array-based object storage has proven to be much slower than an SplObjectStorage instance.
Other methods that I would like to implement include removeAtIndex(integer) and indexOf(object)
It turns out the easiest (and apparently most efficient) way to do this is to extend
SplObjectStorageand to make use ofLimitIterator. Code example below: