- var_dump: documentation ( source)
<?php
class a implements \Iterator, \Countable, \ArrayAccess
{
private $a;
private $b;
private $c;
private $keys = [
'a', 'b', 'c'
];
private $pointer;
public function __construct($a = null, $b = null, $c = null)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
public function count()
{
$pointer = 0;
foreach ($this as $element) {
if ($element) {
$pointer++;
}
}
return $pointer;
}
public function current()
{
$key = $this->key();
return $this->{$key};
}
public function key()
{
return $this->keys[$this->pointer];
}
public function next()
{
$this->pointer++;
}
public function valid()
{
return $this->pointer < count($this->keys);
}
public function rewind()
{
$this->pointer = 0;
}
public function offsetExists($offset)
{
return isset($this->{$offset});
}
public function offsetGet($offset)
{
return $this->{$offset};
}
public function offsetSet($offset, $value)
{
throw new \BadMethodCallException('Object does not implement set');
}
public function offsetUnset($offset)
{
throw new \BadMethodCallException('Value object does not implement modification via unset');
}
}
$me = new a(1,2,3);
var_dump($me->count());
var_dump($me['c']);
foreach ($me as $key => $el) {
if ($key === 'b') {
$me[$key] = 111;
}
}