3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); final class RecursiveFixedArrayProxy implements IteratorAggregate, RecursiveIterator { private SplFixedArray $splFixedArray; public function __construct(iterable $iterable = []) { $this->splFixedArray = new SplFixedArray( is_countable($iterable) ? count($iterable) : iterator_count($iterable) ); $offset = 0; $size = $this->splFixedArray->count(); foreach ($iterable as $key => $value) { if($offset === $size){ // resize the SplFixedArray when nessary. (if the iterable passed in via the __construct implements countable differently eg. where count() returns the capacity/size of the iterable) $this->splFixedArray->setSize(++$size); } if(is_array($value)){ // save arrays as RecursiveFixedArrayProxy $this->splFixedArray->offsetSet($offset++, new self($value)); continue; } // save all other values as stdClass via Closure $this->splFixedArray->offsetSet( $offset++, (static function(mixed $key = null, mixed $value = null): stdClass{ $stdClass = new stdClass(); $stdClass->key = $key; $stdClass->value = $value; return $stdClass; })($key, $value) ); } $this->splFixedArray->setSize($offset); } // required from IteratorAggregate public function getIterator(): Generator { // to provide a Recursive SplFixedArray implemtation. yield from new RecursiveArrayIterator( // is this wrong? trying to avoid using arrays, for memory boost // avoids: $this->splFixedArray->toArray() $this->splFixedArray ); } // required from RecursiveIterator public function getChildren(): ?RecursiveIterator { /** @return RecursiveFixedArray */ return $this->current(); } // required from RecursiveIterator public function hasChildren(): bool { // traverse recursively if *current* is instance of RecursiveFixedArray return $this->current() instanceof RecursiveFixedArray; } }
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26
Fatal error: Class RecursiveFixedArrayProxy cannot implement both Iterator and IteratorAggregate at the same time in /in/usWZK on line 5
Process exited with code 255.
Output for 8.5.0 - 8.5.11
Fatal error: Class RecursiveFixedArrayProxy cannot implement both Iterator and IteratorAggregate at the same time in /in/usWZK on line 5 Stack trace: #0 {main}
Process exited with code 255.

preferences:
34.52 ms | 1140 KiB | 9 Q