Looking at the Pimple source code I found that it is storing objects and their ids in two different arrays:
class Container implements \ArrayAccess
{
private $values = array();
...
private $keys = array();
}
And then:
public function offsetSet($id, $value)
{
...
$this->values[$id] = $value;
$this->keys[$id] = true;
}
And finally:
public function offsetGet($id)
{
if (!isset($this->keys[$id])) {
throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
}
I've also seen something similar in Phalcon source code here.
My question is why to store object id key separately, why not just if (!isset($this->values[$id]))
? Is it faster to search within an array? I did some tests and it seems that search speed is pretty the same.