3v4l.org

run code in 300+ PHP versions simultaneously
<?php class myIterator implements Iterator { public function __construct() { $this->position = 0; } public function rewind() { var_dump(__METHOD__); $this->position = 0; } public function current() { var_dump(__METHOD__); return $this->position; } public function key() { var_dump(__METHOD__); return $this->position; } public function next() { var_dump(__METHOD__); ++$this->position; } public function valid() { var_dump(__METHOD__); return true; } } $i = new LimitIterator(new myIterator, 0, 3); $i = new CachingIterator($i); $i->rewind(); echo "\n1st iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; $i->next(); echo "\n2nd iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; $i->next(); echo "\n3rd iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; $i->next(); echo "\n4th iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; $i->next(); echo "\n5th iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; $i->next();
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Return type of myIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 13 Deprecated: Return type of myIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 23 Deprecated: Return type of myIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 18 Deprecated: Return type of myIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 28 Deprecated: Return type of myIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 8 Deprecated: Creation of dynamic property myIterator::$position is deprecated in /in/aWoPk on line 5 string(18) "myIterator::rewind" string(17) "myIterator::valid" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 1st iteration Valid Current key: 0 Current value: 0 string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 2nd iteration Valid Current key: 1 Current value: 1 string(16) "myIterator::next" 3rd iteration Valid Current key: 2 Current value: 2 4th iteration Not valid Current key: NULL Current value: NULL 5th iteration Not valid Current key: NULL Current value: NULL
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Return type of myIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 13 Deprecated: Return type of myIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 23 Deprecated: Return type of myIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 18 Deprecated: Return type of myIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 28 Deprecated: Return type of myIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 8 Deprecated: Creation of dynamic property myIterator::$position is deprecated in /in/aWoPk on line 5 string(18) "myIterator::rewind" string(17) "myIterator::valid" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 1st iteration Valid Current key: 0 Current value: 0 string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 2nd iteration Valid Current key: 1 Current value: 1 string(16) "myIterator::next" 3rd iteration Valid Current key: 2 Current value: 2 4th iteration Not valid Current key: NULL Current value: NULL 5th iteration Not valid Current key: NULL Current value: NULL
Output for 8.1.0 - 8.1.28
Deprecated: Return type of myIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 13 Deprecated: Return type of myIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 23 Deprecated: Return type of myIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 18 Deprecated: Return type of myIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 28 Deprecated: Return type of myIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/aWoPk on line 8 string(18) "myIterator::rewind" string(17) "myIterator::valid" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 1st iteration Valid Current key: 0 Current value: 0 string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 2nd iteration Valid Current key: 1 Current value: 1 string(16) "myIterator::next" 3rd iteration Valid Current key: 2 Current value: 2 4th iteration Not valid Current key: NULL Current value: NULL 5th iteration Not valid Current key: NULL Current value: NULL
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
string(18) "myIterator::rewind" string(17) "myIterator::valid" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 1st iteration Valid Current key: 0 Current value: 0 string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" 2nd iteration Valid Current key: 1 Current value: 1 string(16) "myIterator::next" 3rd iteration Valid Current key: 2 Current value: 2 4th iteration Not valid Current key: NULL Current value: NULL 5th iteration Not valid Current key: NULL Current value: NULL

preferences:
163.55 ms | 403 KiB | 153 Q