La classe AppendIterator

(PHP 5 >= 5.1.0, PHP 7)

Introduction

Un itérateur qui itère sur plusieurs itérateurs les uns après les autres.

Synopsis de la classe

AppendIterator extends IteratorIterator implements OuterIterator {
/* Méthodes */
public __construct ( void )
public void append ( Iterator $iterator )
public mixed current ( void )
public ArrayIterator getArrayIterator ( void )
public Iterator getInnerIterator ( void )
public int getIteratorIndex ( void )
public scalar key ( void )
public void next ( void )
public void rewind ( void )
public bool valid ( void )
/* Méthodes héritées */
public mixed IteratorIterator::current ( void )
public Traversable IteratorIterator::getInnerIterator ( void )
public scalar IteratorIterator::key ( void )
public void IteratorIterator::next ( void )
public void IteratorIterator::rewind ( void )
public bool IteratorIterator::valid ( void )
}

Sommaire

add a note add a note

User Contributed Notes 4 notes

up
3
joshdifabio at gmail dot com
1 year ago
Note that AppendIterator will segfault when iterating over an empty generator. Do not use AppendIterator in conjunction with generators.

https://3v4l.org/YC68k

https://bugs.php.net/bug.php?id=71436
up
0
koambarun at evolution dot com
6 months ago
$append_iterator = new \AppendIterator();

$generator = ReportModel::come_generator();

foreach ($errors as $value)
{
    //If first $value not empty, generator is not empty.
    if(!empty($value))
    {
        $append_iterator->append($errors);
        //break out of loop after appending.
    break;
    }
}
up
-1
komalbarun at gmail dot com
6 months ago
Updated code.
I could not find how to edit a note :/

Preventing segfault if empty generator.
<?php

$append_iterator
= new \AppendIterator();

$generator = some_generator();

// Only works if first value in generator is not empty
// useful when yielding arrays
foreach ($generator as $value)
{
   
//If first $value not empty, generator is not empty.
   
if(!empty($value))
    {
       
$append_iterator->append($generator );
       
//break out of loop after appending.
       
break;
    }
}
up
-1
komalbarun at gmail dot com
6 months ago
Updated code.
I could not find how to edit a note :/

Preventing segfault if empty generator.
<?php

$append_iterator
= new \AppendIterator();

$generator = ReportModel::come_generator();

// Only works if first value in generator is not empty
// useful when yielding arrays
foreach ($append_iterator as $value)
{
   
//If first $value not empty, generator is not empty.
   
if(!empty($value))
    {
       
$append_iterator->append($errors);
       
//break out of loop after appending.
   
break;
    }
}
To Top