PHP Data Structure push: PhP iterator

Source: Internet
Author: User

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.

01 <?php
02  
03 /**
04 * Simple implementation class of the iterator Mode
05 */
06 class sample
implements Iterator {
07     private
$_items ;
08   
09     public
function __construct(&$data) {
10         $this->_items =
$data;
11     }
12     public
function current() {
13         return
current($this->_items);
14     }
15   
16     public
function next() {
17         next($this->_items);  
18     }
19   
20     public
function key() {
21         return
key($this->_items);
22     }
23   
24     public
function rewind() {
25         reset($this->_items);
26     }
27   
28     public
function valid() {                                                                             
29         return
($this->current() !== FALSE);
30     }
31 }
32   
33 /** DEMO */
34 $data =
array(1, 2, 3, 4, 5);
35 $sa =
new sample($data);
36 foreach ($sa
AS $key
=>
$row) {
37     echo
$key, ' ',
$row, '<br />';
38 }
39  
40 ?>

The usage scope of several iterators is as follows:

  1. When using the returned iterator package or library (such as the SPL iterator in PhP5)
  2. You cannot obtain all elements of a container at one call.
  3. When processing a large amount of non-prime data (the tables in the database are measured in GB)
  4. ......

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:

01 <?php  
02 class sample
implements Iterator
03 {
04     private
$_items = array(1,2,3,4,5,6,7);
05   
06     public
function __construct() {
07                   ;//void
08     }
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 ); }
14 }
15   
16 $sa =
new sample();
17 foreach($sa
as $key
=>
$val){
18     print
$key . "=>"
.$val;
19 }
20 ?>

The while loop can also be:

1 <?php
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
5 }
6 ?>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.