Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 920 Bytes

QueueIteratorTrait.md

File metadata and controls

44 lines (33 loc) · 920 Bytes

Docs / Array / QueueIteratorTrait

Same as IteratorAggregateTrait, except it will consume array content. As a Queue (FIFO) iterator, it will consume from the start of the array and forward.

Trait synopsis

trait QueueIteratorTrait
{
    use TypeTrait;

    // IteratorAggregate interface implementation.

    /**
     * Consume array and yield key/value pair.
     * @return Generator The iterator function.
     */
    public function getIterator(): Generator;
}

Examples

use Phrity\O\Array\QueueIteratorTrait;

class MyClass implements IteratorAggregate, Traversable
{
    use QueueIteratorTrait;

    public function __construct(array $input)
    {
        $this->initialize($input);
    }
}

$class = new MyClass(['a' => 1, 'b' => 2, 'c' => 3]);
foreach ($class as $key => $val) {
    // ... consumes the array content
}
// $class is now empty