Skip to content

Instantly share code, notes, and snippets.

@havvg
Last active August 15, 2017 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havvg/c3fcfd3a7270856257e11695af24d781 to your computer and use it in GitHub Desktop.
Save havvg/c3fcfd3a7270856257e11695af24d781 to your computer and use it in GitHub Desktop.
A simple \Traversable based on an array
<?php
class SimpleArrayTraversable implements \IteratorAggregate
{
private $data;
public function __construct($a, $b, $c)
{
$this->data = [$a, $b, $c];
}
public function getIterator()
{
return (function() {
yield from $this->data;
})();
}
}
$list = new SimpleArrayTraversable('1', '2', '3');
foreach ($list as $key => $value) {
var_dump([$key, $value]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment