<?php declare(strict_types=1); namespace App; use Generator; use CachingIterator; class Foo { static $i = 0; public function __toString() { echo "Foo::_toString called " . (self::$i + 1) . " times\n"; return "foo " . ((string) ++self::$i); } } $generator = /** * @return Generator<bool, bool> */ static function () { yield true => true; yield false => false; yield "foo" => new Foo(); yield "bar" => new Foo(); yield "baz" => new Foo(); }; $iterator = new CachingIterator($generator(), CachingIterator::FULL_CACHE | CachingIterator::CALL_TOSTRING); foreach ($iterator as $k => $v) { var_dump($k, $v); } foreach ($iterator->getCache() as $k => $v) { var_dump($k, $v); }
You have javascript disabled. You will not be able to edit any code.