Voting

: min(eight, eight)?
(Example: nine)

The Note You're Voting On

trumbull dot j at gmail dot com
9 years ago
It might seem obvious, but you can return a compiled generator from your IteratorAggregate::getIterator() implementation.
<?php
class Collection implements IteratorAggregate
{
 private $items = [];
 public function __construct($items = [])
 {
 $this->items = $items;
 }
 public function getIterator()
 {
 return (function () {
 while(list($key, $val) = each($this->items)) {
 yield $key => $val;
 }
 })();
 }
}
$data = [ 'A', 'B', 'C', 'D' ];
$collection = new Collection($data);
foreach ($collection as $key => $val) {
 echo sprintf("[%s] => %s\n", $key, $val);
}
?>

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /