Sie sind auf Seite 1von 16

Downloads

Documentation
Get Involved
Help

Search

Getting Started
Introduction
A simple tutorial
Language Reference
Basic syntax
Types
Variables
Constants
Expressions
Operators
Control Structures
Functions
Classes and Objects
Namespaces
Errors
Exceptions
Generators
References Explained
Predefined Variables
Predefined Exceptions
Predefined Interfaces and Classes
Context options and parameters
Supported Protocols and Wrappers

Security
Introduction
General considerations
Installed as CGI binary
Installed as an Apache module
Session Security
Filesystem Security
Database Security
Error Reporting
Using Register Globals
User Submitted Data
Magic Quotes
Hiding PHP
Keeping Current
Features
HTTP authentication with PHP
Cookies
Sessions
Dealing with XForms
Handling file uploads
Using remote files
Connection handling
Persistent Database Connections
Safe Mode
Command line usage
Garbage Collection
DTrace Dynamic Tracing

Function Reference
Affecting PHP's Behaviour
Audio Formats Manipulation
Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Credit Card Processing
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions

Keyboard Shortcuts
?
This help
j
Next menu item
k
Previous menu item
gp
Previous man page
gn
Next man page
G
Scroll to bottom
gg
Scroll to top
gh
Goto homepage
gs
Goto search
(current page)
/
Focus search box

Iterator::current
Traversable

Manual de PHP
Referencia del lenguaje
Interfaces y clases predefinidas

Change language: Spanish

Edit Report a Bug

La interfaz Iterator
(PHP 5 >= 5.0.0, PHP 7)

Introduccin
Interfaz para iteradores externos u objetos que pueden ser iterados internamente por s
mismos.

Sinopsis de la Interfaz
Iterator extends Traversable {
/* Mtodos */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public boolean valid ( void )
}

Iteradores Predefinidos
PHP ya ofrece un nmero de iteradores para muchas de las tareas del da a da. Vase la
lista de iteradores SPL.

Ejemplos
Ejemplo #1 Uso bsico

Este ejemplo muestra el orden en el que se llaman a los mtodos cuando se emplea un
foreach con un iterator.

<?php
classmyIteratorimplementsIterator{
private$position=0;
private$array=array(
"firstelement",
"secondelement",
"lastelement",
);

publicfunction__construct(){
$this->position=0;
}

functionrewind(){
var_dump(__METHOD__);
$this->position=0;
}

functioncurrent(){
var_dump(__METHOD__);
return$this->array[$this->position];
}

functionkey(){
var_dump(__METHOD__);
return$this->position;
}

functionnext(){
var_dump(__METHOD__);
++$this->position;
}

functionvalid(){
var_dump(__METHOD__);
returnisset($this->array[$this->position]);
}
}

$it=newmyIterator;

foreach($itas$key=>$value){
var_dump($key,$value);
echo"\n";
}
?>

El resultado del ejemplo sera algo similar a:


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"

Tabla de contenidos
Iterator::current Devuelve el elemento actual
Iterator::key Devuelve la clave del elemento actual
Iterator::next Avanza al siguiente elemento
Iterator::rewind Rebobine la Iterator al primer elemento
Iterator::valid Comprueba si la posicin actual es vlido

add a note

User Contributed Notes 13 notes

up
down
120
robert_e_lee at dell dot com
6 years ago
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:

<?php
$it->rewind();

while ($it->valid())
{
$key = $it->key();
$value = $it->current();

// ...

$it->next();
}
?>

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...
up
down
15
fetidfrog at gmail dot com
4 years ago
If you have a custom iterator that may throw an exception in it's current() method, there is no way
to catch the exception without breaking a foreach loop.

The following for loop allows you to skip elements for which $iterator->current() throws an
exception, rather than breaking the loop.

<?php
for ($iterator->rewind(); $iterator->valid(); $iterator->next()) {
try {
$value = $iterator->current();
} catch (Exception $exception) {
continue;
}

# ...
}
?>
up
down
19
RocketInABog at techno-monks dot net
7 years ago
<?php
# - Here is an implementation of the Iterator interface for arrays
# which works with maps (key/value pairs)
# as well as traditional arrays
# (contiguous monotonically increasing indexes).
# Though it pretty much does what an array
# would normally do within foreach() loops,
# this class may be useful for using arrays
# with code that generically/only supports the
# Iterator interface.
# Another use of this class is to simply provide
# object methods with tightly controlling iteration of arrays.

class tIterator_array implements Iterator {


private $myArray;

public function __construct( $givenArray ) {


$this->myArray = $givenArray;
}
function rewind() {
return reset($this->myArray);
}
function current() {
return current($this->myArray);
}
function key() {
return key($this->myArray);
}
function next() {
return next($this->myArray);
}
function valid() {
return key($this->myArray) !== null;
}
}

?>
up
down
11
mike dot thornton at firstroi dot com
7 years ago
It's important to note that following won't work if you have null values.

<?php
function valid() {
var_dump(__METHOD__);
return isset($this->array[$this->position]);
}
?>

Other examples have shown the following which won't work if you have false values:

<?php
function valid() {
return $this->current() !== false;
}
?>

Instead use:

<?php
function valid() {
return array_key_exists($this->array, $this->position);
}
?>

Or the following if you do not store the position.

<?php
public function valid() {
return !is_null(key($this->array));
}
?>
up
down
2
Anonymous
11 months ago
An interesting fact that I didn't read in the doc:

the key() method is called only if your foreach loop needs it.

For instance, the following loop calls the key() method:

<?php
foreach($it as $key => $value) {
var_dump($key, $value);
echo "\n";
}
?>

But the following loop doesn't:

<?php
foreach($it as $value) {
var_dump($value);
echo "\n";
}
?>
up
down
8
Geoffrey Sneddon
7 years ago
So, playing around with iterators in PHP (coming from languages where I'm spoiled with generators to
do things like this), I wrote a quick piece of code to give the Fibonacci sequence (to infinity,
though only the first terms up to F_{10} are output).

<?php

class Fibonacci implements Iterator {


private $previous = 1;
private $current = 0;
private $key = 0;

public function current() {


return $this->current;
}

public function key() {


return $this->key;
}

public function next() {


$newprevious = $this->current;
$this->current += $this->previous;
$this->previous = $newprevious;
$this->key++;
}

public function rewind() {


$this->previous = 1;
$this->current = 0;
$this->key = 0;
}

public function valid() {


return true;
}
}

$seq = new Fibonacci;


$i = 0;
foreach ($seq as $f) {
echo "$f\n";
if ($i++ === 10) break;
}
?>
up
down
6
Anthony Sterling
7 years ago
Here's a Fibonacci example using the formula, rather than addition.

<?php
/**
* @author Anthony Sterling
*/
class FibonacciSequence implements Iterator
{
protected
$limit = 0;

protected
$key = 0;

public function __construct($limit = 0)


{
$this->limit = (integer)$limit;
}

public function current()


{
return round(
(pow(((1 + sqrt(5)) / 2), $this->key) - pow((-1 / (1 + sqrt(5)) / 2), $this->key)) /
sqrt(5),
null
);
}

public function key()


{
return $this->key;
}

public function next()


{
$this->key++;
}

public function rewind()


{
$this->key = 0;
}

public function valid()


{
return $this->key < $this->limit;
}
}

foreach(new FibonacciSequence() as $number)


{
printf(
'%d<br />',
$number
);
}
/*
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
*/
?>

Anthony.
up
down
2
Daniel Lo Nigro
3 years ago
If you're using PHP 5.5 or above and are creating a simple iterator, consider using a generator
function instead. There is significantly less boilerplate code and the code is easier to read.
http://au1.php.net/generators
up
down
1
geompse at gmail dot com
5 years ago
Be carefull with Iterator when using nested loops or deleting items inside the collection while
looping over it.
It can be tricky to detect.
This unexpected behavior is pertinent if you think about it long enough.
<?php

foreach($it as $key => $value)


echo $value;
#output: value1, value2, value3

foreach($it as $key => $value)


foreach($it as $key => $value)
echo $value;
#output: value1, value2, value3

foreach($it as $key => $value)


foreach(clone $it as $key => $value)
echo $value;
#output: value1, value2, value3, value1, value2, value3, value1, value2, value3

foreach($it as $key => $value)


{
echo $value;
array_shift($it->values);
}
#ouput: value1, value3

?>
up
down
0
Gilles A
2 years ago
Examples of use
<?php
class myIterator implements Iterator
{
private
$_array = array();
public function __construct(array $array)
{
$this->_array = $array;
}
public function rewind()
{
reset($this->_array);
}
public function current()
{
return current($this->_array);
}
public function key()
{
return key($this->_array);
}
public function next()
{
next($this->_array);
}
public function valid()
{
return $this->key() !== null;
}
}

$it = new myIterator(array('foo_1' => 'bar_1','foo_2' => 'bar_2'));

//example 1 : foreach

foreach($it as $key => $value)


{
var_dump($key, $value);
}

//example 2 : while

$it -> rewind();


while($it->valid())
{
var_dump($it->key(), $it->current());

$it->next();
}

//example 3 : for

for($it->rewind();$it->valid();$it->next())
{
var_dump($it->key(), $it->current());
}
?>
up
down
-1
ct
5 years ago
up
down
-1
kent at marketacumen dot com
6 years ago
up
down
-3
dwlnetnl at users dot sourceforge dot net
5 years ago
Be aware that when you call a method like current($this) within the Iterator class, the properties of
the class are returned and the Iterator's current() isn't called. This is because current() applies
to arrays and the Iterator class is then interpretated as an array.
add a note

Interfaces y clases predefinidas


Traversable
Iterator
IteratorAggregate
Throwable
ArrayAccess
Serializable
Closure
Generator

Copyright 2001-2017 The PHP Group


My PHP.net
Contact
Other PHP.net sites
Mirror sites
Privacy policy

Das könnte Ihnen auch gefallen