Trait that implements the IteratorAggregate and
Traversable interfaces.
Enables traversing properties with methods such as foreach()
by aggregating a Generator.
Trait synopsis
trait IteratorAggregateTrait
{
use TypeTrait;
// IteratorAggregate interface implementation.
/**
* Iterate object properties and yield key/value pair.
* @return Generator The iterator function.
*/
public function getIterator(): Generator;
}
Examples
use Phrity\O\Object\IteratorAggregateTrait;
class MyClass implements IteratorAggregate, Traversable
{
use IteratorAggregateTrait;
public function __construct(object $input)
{
$this->initialize($input);
}
}
$class = new MyClass((object)['a' => 1, 'b' => 2, 'c' => 3]);
foreach ($class as $key => $val) {
// ...
}
Requirements
PHP
^8.0