iterators_006.phpt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --TEST--
  2. ZE2 iterators and array wrapping
  3. --SKIPIF--
  4. <?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 is needed'); ?>
  5. --FILE--
  6. <?php
  7. class ai implements Iterator {
  8. private $array;
  9. function __construct() {
  10. $this->array = array('foo', 'bar', 'baz');
  11. }
  12. function rewind() {
  13. reset($this->array);
  14. $this->next();
  15. }
  16. function valid() {
  17. return $this->key !== NULL;
  18. }
  19. function key() {
  20. return $this->key;
  21. }
  22. function current() {
  23. return $this->current;
  24. }
  25. function next() {
  26. list($this->key, $this->current) = each($this->array);
  27. // list($key, $current) = each($this->array);
  28. // $this->key = $key;
  29. // $this->current = $current;
  30. }
  31. }
  32. class a implements IteratorAggregate {
  33. public function getIterator() {
  34. return new ai();
  35. }
  36. }
  37. $array = new a();
  38. foreach ($array as $property => $value) {
  39. print "$property: $value\n";
  40. }
  41. #$array = $array->getIterator();
  42. #$array->rewind();
  43. #$array->valid();
  44. #var_dump($array->key());
  45. #var_dump($array->current());
  46. echo "===2nd===\n";
  47. $array = new ai();
  48. foreach ($array as $property => $value) {
  49. print "$property: $value\n";
  50. }
  51. echo "===3rd===\n";
  52. foreach ($array as $property => $value) {
  53. print "$property: $value\n";
  54. }
  55. ?>
  56. ===DONE===
  57. --EXPECT--
  58. 0: foo
  59. 1: bar
  60. 2: baz
  61. ===2nd===
  62. 0: foo
  63. 1: bar
  64. 2: baz
  65. ===3rd===
  66. 0: foo
  67. 1: bar
  68. 2: baz
  69. ===DONE===