3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TransformingIterator implements \Iterator { /** * @var Iterator */ private $inner; /** * @var callable */ private $valTransformCallback; /** * @var callable */ private $keyTransformCallback; /** * Constructor * * @param Iterator|array $inner * @param callable $valTransformCallback * @param callable $keyTransformCallback * @throws \InvalidArgumentException */ public function __construct($inner, callable $valTransformCallback = null, callable $keyTransformCallback = null) { if ($inner instanceof \Iterator) { $this->inner = $inner; } else if (is_array($inner)) { $this->inner = new ArrayIterator($inner); } else { throw new \InvalidArgumentException('$inner must be an Iterator or an array'); } $this->valTransformCallback = $valTransformCallback; $this->keyTransformCallback = $keyTransformCallback; } public function current() { return $this->valTransformCallback ? call_user_func($this->valTransformCallback, $this->inner->current()) : $this->inner->current(); } public function next() { $this->inner->next(); } public function key() { return $this->valTransformCallback ? call_user_func($this->valTransformCallback, $this->inner->key()) : $this->inner->key(); } public function valid() { return $this->inner->valid(); } public function rewind() { $this->inner->rewind(); } } $it = new TransformingIterator([1,2,3], function ($value) { return $value + 1; }); foreach ($it as $val) { echo $val . "\n"; }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of TransformingIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cddms on line 42 Deprecated: Return type of TransformingIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cddms on line 49 Deprecated: Return type of TransformingIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cddms on line 54 Deprecated: Return type of TransformingIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cddms on line 61 Deprecated: Return type of TransformingIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cddms on line 66 2 3 4

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
32.95 ms | 403 KiB | 8 Q