Listing 1537
Submitted by Erik Morén, 31 August 2008
/** * A class that overload elements. * */ abstract class Element implements ArrayAccess { /** * Holds trhe element values * * @var array */ protected $value = array(); /** * Defined by ArrayAccess interface * Set a value given it's key e.g. $object['title'] = 'foo'; * * @param mixed key (string or integer) * @param mixed value */ public function offsetSet($key, $value) { if (isset($this->value[$key])) { $this->value[$key] = $value; } } /** * Defined by ArrayAccess interface * Return a value given it's key e.g. echo $object['title']; * * @param mixed key (string or integer) * @return mixed value */ public function offsetGet($key) { if (isset($this->value[$key])) { return $this->value[$key]; } } /** * Defined by ArrayAccess interface * Unset a value by it's key e.g. unset($object['title']); * * @param mixed key (string or integer) */ public function offsetUnset($key) { if (isset($this->value[$key])) { unset($this->value[$key]); } } /** * Defined by ArrayAccess interface * Check value exists, given it's key e.g. isset($object['title']) * * @param mixed key (string or integer) * @return boolean */ public function offsetExists($key) { return isset($this->value[$key]); } /** * See offsetSet(). * */ public function __set($key, $value) { $this->offsetSet($key, $value); } /** * See offsetGet(). * */ public function __get($key) { return $this->offsetGet($key); } /** * See offsetUnset(). * */ public function __unset($key) { $this->offsetUnset($key); } /** * See offsetExists(). * */ public function __isset($key) { return $this->offsetExists($key); } }
