From 1fb0ef23d7be0d8ecd9604fce16ee1e0842c6ef6 Mon Sep 17 00:00:00 2001 From: AllenJB Date: Tue, 20 Apr 2021 10:28:39 +0100 Subject: [PATCH] Fix #80942: Object iteration / Iterator: Replace duplicated examples with direct links * Object iteration / Iterator: Replace duplicated examples with direct links to the appropriate sections and add cross-links. Closes GH-517. --- language/generators.xml | 10 ++ language/oop5/iterations.xml | 187 ++---------------------------- language/predefined/generator.xml | 5 + language/predefined/iterator.xml | 4 + 4 files changed, 28 insertions(+), 178 deletions(-) diff --git a/language/generators.xml b/language/generators.xml index 8741e29dd85a..470a27046115 100644 --- a/language/generators.xml +++ b/language/generators.xml @@ -593,6 +593,16 @@ class LineIterator implements Iterator { means that the same generator can't be iterated over multiple times: the generator will need to be rebuilt by calling the generator function again. + + + &reftitle.seealso; + + + Object Iteration + + + + diff --git a/language/oop5/iterations.xml b/language/oop5/iterations.xml index ae3260030870..7266838fb1f2 100644 --- a/language/oop5/iterations.xml +++ b/language/oop5/iterations.xml @@ -69,187 +69,18 @@ private => private var visible properties that could be accessed. - - To take it a step further, the Iterator - interface may be implemented. - This allows the object to dictate how it will be iterated and what values will - be available on each iteration. - - - - Object Iteration implementing Iterator - -var = $array; - } - } - - public function rewind() - { - echo "rewinding\n"; - reset($this->var); - } - - public function current() - { - $var = current($this->var); - echo "current: $var\n"; - return $var; - } - - public function key() - { - $var = key($this->var); - echo "key: $var\n"; - return $var; - } - - public function next() - { - $var = next($this->var); - echo "next: $var\n"; - return $var; - } - - public function valid() - { - $key = key($this->var); - $var = ($key !== NULL && $key !== FALSE); - echo "valid: $var\n"; - return $var; - } - -} - -$values = array(1,2,3); -$it = new MyIterator($values); - -foreach ($it as $a => $b) { - print "$a: $b\n"; -} -?> -]]> - - &example.outputs; - - - - - + + &reftitle.seealso; - The IteratorAggregate - interface - can be used as an alternative to implementing all of the - Iterator methods. - IteratorAggregate only requires the - implementation of a single method, - IteratorAggregate::getIterator, which should return - an instance of a class implementing Iterator. + + Generators + Iterator + IteratorAggregate + SPL Iterators + - - - Object Iteration implementing IteratorAggregate - -items); - } - - public function add($value) { - $this->items[$this->count++] = $value; - } -} - -$coll = new MyCollection(); -$coll->add('value 1'); -$coll->add('value 2'); -$coll->add('value 3'); - -foreach ($coll as $key => $val) { - echo "key/value: [$key -> $val]\n\n"; -} -?> -]]> - - &example.outputs; - - value 1] - -next: value 2 -current: value 2 -valid: 1 -current: value 2 -key: 1 -key/value: [1 -> value 2] - -next: value 3 -current: value 3 -valid: 1 -current: value 3 -key: 2 -key/value: [2 -> value 3] - -next: -current: -valid: -]]> - - - - - - - For more examples of iterators, see the - SPL Extension. - - - - - - Generators provide an - alternative way of defining iterators. - - + +
+ &reftitle.seealso; + See also object iteration. +