Can't write new elements to IteratorAggregate?

Saviour

New Member
Am I correct in thinking that \[code\]IteratorAggregate\[/code\] only provides array-like read access to an object? If I need to write to the object-as-array, then I need to use \[code\]Iterator\[/code\]?Demonstration of an IteratorAggregate object causing a fatal error when trying to add a new element follows:\[code\]<?class foo implements IteratorAggregate { public $_array = array('foo'=>'bar', 'baz'=>'quux'); public function getIterator() { return new ArrayIterator($this->_array); }} // end class declaration$objFoo = & new foo();foreach ( $objFoo as $key => $value ) { echo "key: $key, value: $value\n";}$objFoo['reeb'] = "roob";foreach ( $objFoo as $key => $value ) { echo "key: $key, value: $value\n";}\[/code\]\[code\]$ php test.phpkey: foo, value: barkey: baz, value: quuxFatal error: Cannot use object of type foo as array in /var/www/test.php on line 20Call Stack: 0.0009 57956 1. {main}() /var/www/test.php:0\[/code\]
 
Back
Top