Skip to content

Iterable

What a class implements so a foreach loop can walk it, the way it walks an array.

An Iterable promises one method, iterate, which hands the loop something that steps through the values one at a time. You fix the value type when you declare the class — implements Iterable<string> for a sequence of strings — and every loop over it sees that type. The simplest iterate is one that yields each value in turn; the language builds the stepper from it.

Each loop asks for a fresh stepper, so the same object can be walked more than once, and two loops over it can nest. A loop over an Iterable binds the value alone: there is no key.

Good to know: a class is walkable only because it declares this interface. Nothing else gets a foreach in — not a property walk, and not a count turned into a method call.

Examples

A delivery manifest you can walk twice

<?nvs
// A delivery manifest you can walk with a plain `foreach`, as often as you like.
// The class says `implements Iterable<Parcel>`, so every loop over it knows the
// value it hands you is a `Parcel`.
class Parcel
{
public string $label;
public uint $grams;
public function constructor(string $label, uint $grams)
{
$this->label = $label;
$this->grams = $grams;
}
}
class Manifest implements Iterable<Parcel>
{
private array<Parcel> $parcels;
public function constructor()
{
$this->parcels = [];
}
public function add(string $label, uint $grams): void
{
$this->parcels[] = new Parcel($label, $grams);
}
// The one method the interface asks for. Handing each parcel out with
// `yield` is enough, so there is no second class to write.
public function iterate(): Iterator<Parcel>
{
foreach ($this->parcels as Parcel $parcel) {
yield $parcel;
}
}
}
var $today = new Manifest();
$today->add('Bakery, 12 Mill Lane', 1200);
$today->add('Garage, 4 North Road', 8400);
$today->add('School, 60 Elm Street', 2300);
// First walk: the loading list the driver takes with them.
foreach ($today as Parcel $parcel) {
echo $parcel->label, ' (', $parcel->grams, " g)\n";
}
// Second walk over the same object. Each `foreach` asks the manifest to start
// again, so there is nothing to reset in between.
uint $total = 0;
foreach ($today as Parcel $parcel) {
$total = $total + $parcel->grams;
}
echo 'loaded weight: ', $total, " g\n";
uint $limit = 10000;
if ($total > $limit) {
echo 'too heavy for the small van by ', $total - $limit, " g\n";
}
Output
Bakery, 12 Mill Lane (1200 g)
Garage, 4 North Road (8400 g)
School, 60 Elm Street (2300 g)
loaded weight: 11900 g
too heavy for the small van by 1900 g