_data = array( 'first' => (1), 'second' => (2), 'third' => (3), 'fourth' => (4) ); } public function __get($key) { $value = isset($this->_data[$key]) ? $this->_data[$key] : null; return $value; } public function __set($key, $value) { $this->_data[$key] = $value; } public function __isset($key) { $result = isset($this->_data[$key]); return $result; } public function count() { return count($this->_data); } public function current() { $value = current($this->_data); return $value; } public function key() { return key($this->_data); } public function next() { next($this->_data); } public function rewind() { reset($this->_data); } public function valid() { $key = key($this->_data); $end = ($key === null); return !$end; } public function __unset($key) { unset($this->_data[$key]); } } function testOne($keyToUnset) { echo "Test by unsetting '$keyToUnset':\n\n"; $class = new ArrayClass(); foreach ($class as $key => $value) { echo "start of loop: $key\n"; if ($key == $keyToUnset) { echo " __unset() called\n"; unset($class->$key); } echo "arbitary access of an element in the array: ". $class->second. "\n"; } echo "\nNumber of elements: "; echo count($class); echo "\n"; echo "\nSimple iteration after unset:\n"; foreach ($class as $key => $value) { echo " $key\n"; } echo "\nisset first? "; var_dump(isset($class->first)); echo "isset second? "; var_dump(isset($class->second)); echo "isset third? "; var_dump(isset($class->third)); echo "isset fourth? "; var_dump(isset($class->fourth)); echo "\n"; } function testTwo() { echo "--- Secondary test ---\n"; $class = new ArrayClass(); foreach ($class as $key => $value) { echo " $key\n"; } echo "--\n"; unset($class->first); foreach ($class as $key => $value) { echo " $key\n"; } } testOne('first'); testOne('fourth'); testTwo();