<?php
class MyArrayIter implements Iterator {
private $storage;
public function __construct(array $a) { $storage = $a; }
public function current() {
return current($this->storage);
}
public function key() {
return key($this->storage);
}
public function next() {
next($this->storage);
}
public function rewind() {
reset($this->storage);
}
public function valid() {
return key($this->storage) !== null;
}
}
$i = 1;
$a = [1,2,$i,4,5];
$iter = new MyArrayIter($a);
foreach($iter as &$v) {
echo $v;
}
var_dump($a);
var_dump($iter);
Deprecated: Return type of MyArrayIter::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/nEOeE on line 7
Deprecated: Return type of MyArrayIter::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/nEOeE on line 13
Deprecated: Return type of MyArrayIter::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/nEOeE on line 10
Deprecated: Return type of MyArrayIter::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/nEOeE on line 19
Deprecated: Return type of MyArrayIter::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/nEOeE on line 16
Fatal error: Uncaught Error: An iterator cannot be used with foreach by reference in /in/nEOeE:28
Stack trace:
#0 {main}
thrown in /in/nEOeE on line 28
Process exited with code 255.
Fatal error: Uncaught Error: An iterator cannot be used with foreach by reference in /in/nEOeE:28
Stack trace:
#0 {main}
thrown in /in/nEOeE on line 28
Process exited with code 255.