note 111175 deleted from class.arrayaccess by danbrown
| From: | danbrown@php.net | Date: | Tue, 22 Jan 2013 18:37:40 +0000 |
| Subject: | note 111175 deleted from class.arrayaccess by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193348@lists.php.net to get a copy of this message | ||
Note Submitter: aFavourForPHP at inspirito dot de
----
The reason of the following implementation is that i want to avoid warnings if an array element is
not set. The constructor gets a variable named $useIfValueNotSet which is the default that will be
used when an element is not set.
I did not extend other parts, only the part related to the stuff above.
Code (extends code from http://de1.php.net/manual/en/class.arrayaccess.php#97163):
class MyArray implements arrayaccess
{
/**
*
*/
protected $_useIfValueNotSet = '';
/**
* Constructor
*/
public function __construct($useIfValueNotSet = '')
{
$this->_useIfValueNotSet = $useIfValueNotSet;
}
public function set($name, $value)
{
$this->{$name} = $value;
}
/**
* Uses a default if element is not set
*/
public function get($name)
{
return true === isset($this->{$name})
? $this->{$name} : $this->_useIfValueNotSet;
}
public function offsetGet($offset)
{
return $this->get($offset);
}
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
public function offsetExists($offset)
{
return isset($this->{$offset});
}
public function offsetUnset($offset)
{
unset($this->{$offset});
}
}