PHP Tutorial |
|
2024-11-28 |
|
|
35 |
|
|
<?php
class misc_class {
public $var1 = "abc";
public $var2 = 123;
public $var3 = "def";
private $var4 = 456;
}
$test = new misc_class;
foreach($test as $key=>$value)
echo "$key = $value<br />\n";
?>
Output
var1 = abc
var2 = 123
var3 = def
You can overload PHP's internal iterator by redefining the rewind(), current(), key(), next() and valid() methods like this:
<?php
class MyIterator implements Iterator {
private $var = array();
public function __construct($array) {
if (is_array($array)) {
$this->var = $array;
}
}
public function rewind() {
echo "rewinding<br />\n";
reset($this->var);
}
public function current() {
$var = current($this->var);
echo "current: $var<br />\n";
return $var;
}
public function key() {
$var = key($this->var);
echo "key: $var<br />\n";
return $var;
}
public function next() {
$var = next($this->var);
echo "next: $var<br />\n";
return $var;
}
public function valid() {
$var = $this->current() !== false;
echo "valid: {$var}<br />\n";
return $var;
}
}
$it = new MyIterator(array(1,2,3));
foreach ($it as $key=>$value) {
echo "$key: $value<br />\n";
}
?>
Output
rewinding
current: 1
valid: 1
current: 1
key: 0
0: 1
next: 2
current: 2
valid: 1
current: 2
key: 1
1: 2
next: 3
current: 3
valid: 1
current: 3
key: 2
2: 3
next:
current:
valid:
If you just want to override some of them, use the IteratorAggregate class.