PHP iterator: helps construct specific objects that can provide a single standard interface to loop or iterate any type of counter data. (Not very common, in PHP) use cases: 1. access the content of an aggregate object without exposing its internal representation. 2. support for multiple aggregate objects... SyntaxHighlighter. all ();
PHP iterator:
It can help construct specific objects that can provide a single standard interface to loop or iterate any type of counter data. (Not very common, in PHP)
Use cases:
1. access the content of an aggregate object without exposing its internal representation.
2. supports multiple traversal of aggregate objects.
3. provides a unified interface (that is, multi-state iteration) for traversing different aggregation structures ).
PHP code implementation:
[Php]
// Iterator: helps construct specific objects that can provide a single standard interface to loop or iterate any type of counter data
Class MyIterator implements Iterator {
Private $ var = array ();
Public function _ construct ($ array ){
$ This-> var = $ array;
}
Public function rewind (){
Reset ($ this-> var );
}
Public function current (){
$ Var = current ($ this-> var );
Return $ var;
}
Public function valid (){
$ Var = $ this-> current ()! = False;
Return $ var;
}
Public function next (){
$ Var = next ($ this-> var );
Return $ var;
}
Public function key (){
$ Var = key ($ this-> var );
Return $ var;
}
}
$ Values = array ('A', 'B', 'C ');
$ It = new MyIterator ($ values );
Foreach ($ it as $ a =>$ B ){
Print "$ a: $ B
";
}
?>
Author: initphp