mirror of
https://github.com/php/php-src.git
synced 2024-11-26 19:33:55 +08:00
902d64390e
Writing to a proprety that hasn't been declared is deprecated, unless the class uses the #[AllowDynamicProperties] attribute or defines __get()/__set(). RFC: https://wiki.php.net/rfc/deprecate_dynamic_properties
85 lines
1.3 KiB
PHP
85 lines
1.3 KiB
PHP
--TEST--
|
|
ZE2 iterators and array wrapping
|
|
--FILE--
|
|
<?php
|
|
|
|
class ai implements Iterator {
|
|
|
|
private $array;
|
|
private $key;
|
|
private $current;
|
|
|
|
function __construct() {
|
|
$this->array = array('foo', 'bar', 'baz');
|
|
}
|
|
|
|
function rewind(): void {
|
|
reset($this->array);
|
|
$this->next();
|
|
}
|
|
|
|
function valid(): bool {
|
|
return $this->key !== NULL;
|
|
}
|
|
|
|
function key(): mixed {
|
|
return $this->key;
|
|
}
|
|
|
|
function current(): mixed {
|
|
return $this->current;
|
|
}
|
|
|
|
function next(): void {
|
|
$this->key = key($this->array);
|
|
$this->current = current($this->array);
|
|
next($this->array);
|
|
}
|
|
}
|
|
|
|
class a implements IteratorAggregate {
|
|
|
|
public function getIterator(): Traversable {
|
|
return new ai();
|
|
}
|
|
}
|
|
|
|
$array = new a();
|
|
|
|
foreach ($array as $property => $value) {
|
|
print "$property: $value\n";
|
|
}
|
|
|
|
#$array = $array->getIterator();
|
|
#$array->rewind();
|
|
#$array->valid();
|
|
#var_dump($array->key());
|
|
#var_dump($array->current());
|
|
echo "===2nd===\n";
|
|
|
|
$array = new ai();
|
|
|
|
foreach ($array as $property => $value) {
|
|
print "$property: $value\n";
|
|
}
|
|
|
|
echo "===3rd===\n";
|
|
|
|
foreach ($array as $property => $value) {
|
|
print "$property: $value\n";
|
|
}
|
|
|
|
?>
|
|
--EXPECT--
|
|
0: foo
|
|
1: bar
|
|
2: baz
|
|
===2nd===
|
|
0: foo
|
|
1: bar
|
|
2: baz
|
|
===3rd===
|
|
0: foo
|
|
1: bar
|
|
2: baz
|