The iterator, also known as cursor, is a software design pattern for programming. It can be accessed over container objects (such as list or vector, designers do not need to care about the content of container objects.
Different Languages implement iterator in different ways. Some object-oriented languages such as Java, C #, Python, and Delphi have integrated the features of iterator into the language perfectly, we call it the implicit iterator, but C ++ language itself does not have the iterator feature, but STL still uses the template to implement a powerful iterator.
PhP5 supports interfaces and has built-in iterator interfaces. Therefore, if you define a class and implement the iterator interface, your class object is zend_iter_object. Otherwise, zend_iter_plain_object.
For the class zend_iter_plain_object, foreach obtains the default attribute array of the object through hash_of, and then performs foreach on the array.
For the class object of zend_iter_object, foreach is performed by calling the iterator Interface related functions implemented by the object.
04 |
* Simple implementation class of the iterator Mode |
06 |
class sample
implements Iterator { |
09 |
public
function __construct(& $data ) { |
12 |
public
function current() { |
13 |
return
current( $this ->_items); |
16 |
public
function next() { |
20 |
public
function key() { |
21 |
return
key( $this ->_items); |
24 |
public
function rewind () { |
28 |
public
function valid() {
|
29 |
return
( $this ->current() !== FALSE); |
34 |
$data =
array (1, 2, 3, 4, 5); |
35 |
$sa =
new sample( $data ); |
36 |
foreach ( $sa
AS $key =>
$row ) { |
37 |
echo
$key , ' ' ,
$row , '<br />' ; |
The usage scope of several iterators is as follows:
- When using the returned iterator package or library (such as the SPL iterator in PhP5)
- You cannot obtain all elements of a container at one call.
- When processing a large amount of non-prime data (the tables in the database are measured in GB)
- ......
Different iterators have different interfaces. For example, php spl iterators include next () (move to the next element), corrent () (return the current element), and valid () (check the end of the iteration), rewind () (start from scratch), key () (return the index of the current element ). Of course, you can write the iterator suitable for your own use, or you can use the iterator in the system.
Generally, foreach is used to use the iterator. The following code is compiled:
02 |
class sample
implements Iterator |
04 |
private
$_items = array (1,2,3,4,5,6,7); |
06 |
public
function __construct() { |
09 |
public
function rewind () { reset( $this ->_items); } |
10 |
public
function current() { return
current( $this ->_items); } |
11 |
public
function key() { return
key( $this ->_items); } |
12 |
public
function next() { return
next( $this ->_items); } |
13 |
public
function valid() { return
( $this ->current() !== false ); } |
17 |
foreach ( $sa
as $key =>
$val ){ |
18 |
print
$key . "=>"
. $val ; |
The while loop can also be:
2 |
while ( $itertor ->valid()){ // Determine whether it is the final element |
3 |
$element = $itertor ->current(); // Obtain the current element |
4 |
$itertor ->next(); // Move to the next element |
Why Learning PHP iterators? There is a very important reason: the PHP iterator can use object-oriented to implement common data structures, such as lists, stacks, queues, and graphs. A topic will be created later. php will be used to implement most of the data structures and be oriented to objects. So I preheated the PHP iterator first.