note 99585 added to class.arrayaccess
| From: | pas dot svijeta at gmail dot com | Date: | Wed, 25 Aug 2010 11:21:06 +0000 |
| Subject: | note 99585 added to class.arrayaccess | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172042@lists.php.net to get a copy of this message | ||
I think this improves uramihsayihok's RecursiveArrayClass class. Here, there's no need for
private $data = array() property declaration; every property is placed in the object with public
visibility, and can be accessed directly. The protected $properties declaration here serves as a
container for publicly set property names, i.e. as index, and iterator distinguishes those
properties from per-class-declared properties (public or privete) with the
$this->properties = array_keys(array_diff_key(get_object_vars($this),
get_class_vars(get_class($this))));
statement, so every property declared within the class isnot taken into account while iterating (a
way to "protect" class-declared properties). Further, every array-like argument in the
multidimensional array is transformed (recursion) to ArrAccesIterator object, allowing consistent
approach to the object properties with foreach construct (see example below). Also, toArray() member
method transforms every object of ArrAccessIterator back to array recursively, thus providing the
consistency of structure.
Notation $this->{$offset} allows indexed (nonhash) arrays to be manipulated with consistently
with respect to array keys.
class ArrAccessIterator implements Countable, Iterator, ArrayAccess
{
protected $properties;
public function __construct(array $data = array())
{
foreach ($data as $key => $value)
$this->{$key} = is_array($value) ? new self($value) : $value;
$this->properties = array_keys(array_diff_key(get_object_vars($this),
get_class_vars(get_class($this))));
}
public function __clone()
{
foreach ($this as $key => $value)
if ($value instanceof self) $this->{$key} = clone $value;
}
public function count()
{
return count(array_keys(array_diff_key(get_object_vars($this),
get_class_vars(get_class($this)))));
}
public function offsetSet($offset, $value)
{
$this->{$offset} = is_array($value) ? new self($value) : $value;
$this->properties = array_keys(array_diff_key(get_object_vars($this),
get_class_vars(get_class($this))));
}
public function offsetGet($offset)
{
return $this->{$offset};
}
public function offsetExists($offset)
{
return isSet($this->{$offset});
}
public function offsetUnset($offset)
{
unset($this->{$offset});
$this->properties = array_keys(array_diff_key(get_object_vars($this),
get_class_vars(get_class($this))));
}
public function rewind()
{
reset($this->properties);
}
public function valid()
{
return !is_null(key($this->properties));
}
public function key()
{
return current($this->properties);
}
public function current()
{
return $this->{$this->key()};
}
public function next()
{
next($this->properties);
}
}
/* example */
$q = array(1, 2, 3, 4, array(12, 13, 14, 15), 6, 7, 8, array(21, 22, 23, 24, array(31, 32, 33,
array(44, 45), 35, 36), 26, 27), 10);
$a_a_i = new ArrAccessIterator($q);
function nasty_recursive($a_a_i, $init = false)
{
static $margin_left = 20;
print '<div style="margin-left:'.strval($margin_left).'px;">';
foreach ($a_a_i as $k => $v) {
print $k.'=>';
$v instanceof Traversable ? nasty_recursive($v) : print $v.'<br />';
}
print '</div>';
$init ? ($margin_left = 20) : ($margin_left += 10);
}
nasty_recursive($a_a_i, true);
unset($a_a_i[4][2]);
print '-------------------';
nasty_recursive($a_a_i, true);
$a_a_i[10] = array('a', 'b', 'c');
print '-------------------';
nasty_recursive($a_a_i, true);
print '-------------------';
print '<br />level 0 count: '.count($a_a_i);
print '<br />level 8 - 4 count: '.count($a_a_i[8][4]);
----
Server IP: 193.198.184.18
Probable Submitter: 93.143.37.119
----
Manual Page -- http://www.php.net/manual/en/class.arrayaccess.php
Edit -- https://master.php.net/note/edit/99585
Del: integrated -- https://master.php.net/note/delete/99585/integrated
Del: useless -- https://master.php.net/note/delete/99585/useless
Del: bad code -- https://master.php.net/note/delete/99585/bad+code
Del: spam -- https://master.php.net/note/delete/99585/spam
Del: non-english -- https://master.php.net/note/delete/99585/non-english
Del: in docs -- https://master.php.net/note/delete/99585/in+docs
Del: other reasons-- https://master.php.net/note/delete/99585
Reject -- https://master.php.net/note/reject/99585
Search -- https://master.php.net/manage/user-notes.php