update page now

Voting

: zero plus seven?
(Example: nine)

The Note You're Voting On

Martin Speer
6 years ago
You can use yield from in getIterator in recent PHP 7 versions: 

<?php 

class Example implements \IteratorAggregate
{
  protected $data = [];

  public function __construct(array $data) 
  {
    $this->data = $data;
  }

  public function getIterator() 
  {
    yield from $this->data;
  }
}

$test = new Example([1, 2, 3]);

foreach ($test as $node) {
  echo $test, PHP_EOL;
}

/*
 * Outputs:
 *
 * 1
 * 2
 * 3 
 */ ?>

<< Back to user notes page

To Top