CHAPTER 12 ■ SPL ARRAY OVERLOADING^187
parent::__construct(&$this->_values);
}
public function offsetSet($key, $value) {
$this->_keys[spl_object_hash($key)] = $key;
$this->_values[spl_object_hash($key)] = $value;
}
public function offsetGet($key) {
return $this->_values[spl_object_hash($key)];
}
public function offsetExists($key) {
return array_key_exists(spl_object_hash($key), $this->_values);
}
public function offsetUnset($key) {
unset($this->_values[spl_object_hash($key)]);
}
public function getKey($hash) {
return $this->_keys[$hash];
}
}
$key = new KeyObject();
$collection = new CollectionObject();
$collection[$key] = 'test';
foreach($collection as $k => $v) {
print_r($collection->getKey($k));
print_r($v);
}
KeyObject Object
(
)
test
■Note When calling the parent ArrayIterator constructor, be sure to pass the $_values array as a
reference. This will ensure that future updates to the collection are reflected in the iterator.
McArthur_819-9C12.fm Page 187 Thursday, February 28, 2008 7:51 AM