Hướng dẫn what is an iterator in php? - trình lặp trong php là gì?

Giao diện vòng lặp

[Php 5, Php 7, Php 8]

Giới thiệu

Giao diện cho các trình lặp bên ngoài hoặc các đối tượng có thể tự lặp lại trong nội bộ.

Bản tóm tắt giao diện

Xác định trước

PHP đã cung cấp một số trình lặp trong nhiều nhiệm vụ hàng ngày. Xem SPL Iterators cho một danh sách.

Ví dụ

Ví dụ số 1 sử dụng cơ bản

Ví dụ này chứng minh trong đó các phương thức đặt hàng được gọi khi sử dụng foreach với một trình lặp.

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

string[18] "myIterator::rewind"
string[17] "myIterator::valid"
string[19] "myIterator::current"
string[15] "myIterator::key"
int[0]
string[12] "firstelement"

string[16] "myIterator::next"
string[17] "myIterator::valid"
string[19] "myIterator::current"
string[15] "myIterator::key"
int[1]
string[13] "secondelement"

string[16] "myIterator::next"
string[17] "myIterator::valid"
string[19] "myIterator::current"
string[15] "myIterator::key"
int[2]
string[11] "lastelement"

string[16] "myIterator::next"
string[17] "myIterator::valid"

Mục lục

  • Iterator :: hiện tại - trả về phần tử hiện tại
  • Iterator :: phím - Trả lại khóa của phần tử hiện tại
  • Iterator :: Tiếp theo - Chuyển tiếp đến phần tử tiếp theo
  • Iterator :: tua lại - tua lại trình lặp lại phần tử đầu tiên
  • Iterator :: hợp lệ - kiểm tra xem vị trí hiện tại có hợp lệ không

Robert_e_lee tại Dell Dot Com ¶

12 năm trước

Order of operations when using a foreach loop:

1. Before the first iteration of the loop, Iterator::rewind[] is called.
2. Before each iteration of the loop, Iterator::valid[] is called.
3a. It Iterator::valid[] returns false, the loop is terminated.
3b. If Iterator::valid[] returns true, Iterator::current[] and
Iterator::key[] are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next[] is called and we repeat from step 2 above.

This is roughly equivalent to:



The loop isn't terminated until Iterator::valid[] returns false or the body of the loop executes a break statement.

The only two methods that are always executed are Iterator::rewind[] and Iterator::valid[] [unless rewind throws an exception].

The Iterator::next[] method need not return anything. It is defined as returning void. On the other hand, sometimes it is convenient for this method to return something, in which case you can do so if you want.

If your iterator is doing something expensive, like making a database query and iterating over the result set, the best place to make the query is probably in the Iterator::rewind[] implementation.

In this case, the construction of the iterator itself can be cheap, and after construction you can continue to set the properties of the query all the way up to the beginning of the foreach loop since the
Iterator::rewind[] method isn't called until then.

Things to keep in mind when making a database result set iterator:

* Make sure you close your cursor or otherwise clean up any previous query at the top of the rewind method. Otherwise your code will break if the same iterator is used in two consecutive foreach loops when the first loop terminates with a break statement before all the results are iterated over.

* Make sure your rewind[] implementation tries to grab the first result so that the subsequent call to valid[] will know whether or not the result set is empty. I do this by explicitly calling next[] from the end of my rewind[] implementation.

* For things like result set iterators, there really isn't always a "key" that you can return, unless you know you have a scalar primary key column in the query. Unfortunately, there will be cases where either the iterator doesn't know the primary key column because it isn't providing the query, the nature of the query is such that a primary key isn't applicable, the iterator is iterating over a table that doesn't have one, or the iterator is iterating over a table that has a compound primary key. In these cases, key[] can return either:
the row index [based on a simple counter that you provide], or can simply return null.

Iterators can also be used to:

* iterate over the lines of a file or rows of a CSV file
* iterate over the characters of a string
* iterate over the tokens in an input stream
* iterate over the matches returned by an xpath expression
* iterate over the matches returned by a regexp
* iterate over the files in a folder
* etc...

Rocketinabog tại Techno-Monks Dot Net

13 năm trước

0

__construct[] {
        
$this->position 0;
    }

    public function

rewind[]: void {
        
var_dump[__METHOD__];
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current[] {
        
var_dump[__METHOD__];
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key[] {
        
var_dump[__METHOD__];
        return 
$this->position;
    }

    public function

next[]: void {
        
var_dump[__METHOD__];
        ++
$this->position;
    }

    public function

valid[]: bool {
        
var_dump[__METHOD__];
        return isset[
$this->array[$this->position]];
    }
}
$it = new myIterator;

foreach[

$it as $key => $value] {
    
var_dump[$key$value];
    echo 
"\n";
}
?>
1

__construct[] {
        
$this->position 0;
    }

    public function

rewind[]: void {
        
var_dump[__METHOD__];
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current[] {
        
var_dump[__METHOD__];
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key[] {
        
var_dump[__METHOD__];
        return 
$this->position;
    }

    public function

next[]: void {
        
var_dump[__METHOD__];
        ++
$this->position;
    }

    public function

valid[]: bool {
        
var_dump[__METHOD__];
        return isset[
$this->array[$this->position]];
    }
}
$it = new myIterator;

foreach[

$it as $key => $value] {
    
var_dump[$key$value];
    echo 
"\n";
}
?>
2

4

__construct[] {
        
$this->position 0;
    }

    public function

rewind[]: void {
        
var_dump[__METHOD__];
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current[] {
        
var_dump[__METHOD__];
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key[] {
        
var_dump[__METHOD__];
        return 
$this->position;
    }

    public function

next[]: void {
        
var_dump[__METHOD__];
        ++
$this->position;
    }

    public function

valid[]: bool {
        
var_dump[__METHOD__];
        return isset[
$this->array[$this->position]];
    }
}
$it = new myIterator;

foreach[

$it as $key => $value] {
    
var_dump[$key$value];
    echo 
"\n";
}
?>
5

7

__construct[] {
        
$this->position 0;
    }

    public function

rewind[]: void {
        
var_dump[__METHOD__];
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current[] {
        
var_dump[__METHOD__];
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key[] {
        
var_dump[__METHOD__];
        return 
$this->position;
    }

    public function

next[]: void {
        
var_dump[__METHOD__];
        ++
$this->position;
    }

    public function

valid[]: bool {
        
var_dump[__METHOD__];
        return isset[
$this->array[$this->position]];
    }
}
$it = new myIterator;

foreach[

$it as $key => $value] {
    
var_dump[$key$value];
    echo 
"\n";
}
?>
8

__construct[] {
        
$this->position 0;
    }

    public function

rewind[]: void {
        
var_dump[__METHOD__];
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current[] {
        
var_dump[__METHOD__];
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key[] {
        
var_dump[__METHOD__];
        return 
$this->position;
    }

    public function

next[]: void {
        
var_dump[__METHOD__];
        ++
$this->position;
    }

    public function

valid[]: bool {
        
var_dump[__METHOD__];
        return isset[
$this->array[$this->position]];
    }
}
$it = new myIterator;

foreach[

$it as $key => $value] {
    
var_dump[$key$value];
    echo 
"\n";
}
?>
9

Order of operations when using a foreach loop:0

fetidfrog tại gmail dot com ¶

10 năm trước

Order of operations when using a foreach loop:1

Order of operations when using a foreach loop:2

Order of operations when using a foreach loop:3

Bài Viết Liên Quan

Chủ Đề