PHP 5 provides a way to define objects so that they can be traversed through a list of cells, such as using the foreach statement. By default, all visible properties will be used for traversal.
Example #1 Simple object traversal
class MyClass { public $var1 = 1; public $var2 = 2; public $var3 = 3; protected $protected = 'protected var'; private $private = 'private var'; function iterateVisible(){ echo "MyClass::iterateVisible:<br>"; foreach($this as $key => $value){ print "$key=>$value<br>"; } } } $class = new MyClass(); foreach($class as $key => $value){ print "$key => $value<br>"; } echo "<br>"; $class->iterateVisible();
Output result:
var1 => 1
var2 => 2
var3 => 3
MyClass::iterateVisible:
var1=>1
var2=>2
var3=>3
protected=>protected var
private=>private var
As shown above, foreach traverses all the visible properties it can access.
Going a step further, you can implement the Iterator interface. You can let the object decide for itself how to traverse and which values are available each time it is traversed.
class MyIterator implements Iterator{ private $var = array(); public function __construct($array) { if(is_array($array)){ $this->var = $array; } } public function rewind(){ echo "rewinding<br>"; reset($this->var); } public function current(){ $var = current($this->var); echo "current:$var<br>"; return $var; } public function key(){ $var = key($this->var); echo "key:$var<br>"; return $var; } public function next(){ $var = next($this->var); echo "next:$var<br>"; return $var; } public function valid(){ $var = $this->current()!==false; echo "valid:$var<br>"; return $var; } } $values = array(1,2,3); $it = new MyIterator($values); foreach($it as $a => $b){ print "$a:$b<br>"; }
Output result:
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:
You can use the IteratorAggregate interface instead of implementing all Iterator methods. IteratorAggregate only needs to implement one method, IteratorAggregate::getIterator(), which should return an instance of the class that implements Iterator.
Example #3 Traverse objects by implementing IteratorAggregate
include_once('class2.php'); class MyCollection implements IteratorAggregate { private $items = array(); private $count = 0; public function getIterator(){ return new MyIterator($this->items); } public function add($value){ $this->items[$this->count++] = $value; } } $coll = new MyCollection(); $coll -> add('1'); $coll -> add('2'); $coll -> add('3'); foreach($coll as $k => $v){ echo "key/value:[$k->$v]<br><br>"; }
Output result:
rewinding
current:1
valid:1
current:1
key:0
key/value:[0-> ;1]
next:2
current:2
valid:1
current:2
key:1
key/value:[1->2]
next:3
current:3
valid:1
current:3
key:2
key/value:[2->3]
next:
current:
valid: