Last active
August 15, 2017 16:19
-
-
Save havvg/c3fcfd3a7270856257e11695af24d781 to your computer and use it in GitHub Desktop.
A simple \Traversable based on an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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