PhpRiot
Recent Code Pastes

Listing 1538

Followup to listing-1537.php, submitted by Erik Morén.

Submitted by Erik Morén, 31 August 2008
<?php
/**
 * 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]);
    }
}
Submit a Follow Up