]> granicus.if.org Git - php/commit
SplFixedArray is Aggregate, not Iterable
authorAlex Dowad <alexinbeijing@gmail.com>
Mon, 11 May 2020 18:32:13 +0000 (20:32 +0200)
committerAlex Dowad <alexinbeijing@gmail.com>
Wed, 23 Sep 2020 06:33:24 +0000 (08:33 +0200)
commit4222ae16e7848e0b3f062a9a989d387de307a7af
tree049e87edb7c167065cf8399943323d4451dc69cd
parent1e9db80d7264911fa4089cb7e4b3dc7f97b19c6e
SplFixedArray is Aggregate, not Iterable

One strange feature of SplFixedArray was that it could not be used in nested foreach
loops. If one did so, the inner loop would overwrite the iteration state of the outer
loop.

To illustrate:

    $spl = SplFixedArray::fromArray([0, 1]);
    foreach ($spl as $a) {
      foreach ($spl as $b) {
        echo "$a $b";
      }
    }

Would only print two lines:

    0 0
    0 1

Use the new InternalIterator feature which was introduced in ff19ec2df3 to convert
SplFixedArray to an Aggregate rather than Iterable. As a bonus, we get to trim down
some ugly code! Yay!
NEWS
UPGRADING
ext/spl/spl_fixedarray.c
ext/spl/spl_fixedarray.stub.php
ext/spl/spl_fixedarray_arginfo.h
ext/spl/tests/SplFixedArray_change_size_during_iteration.phpt [new file with mode: 0644]
ext/spl/tests/SplFixedArray_key_setsize.phpt [deleted file]
ext/spl/tests/SplFixedArray_nested_foreach.phpt [new file with mode: 0644]
ext/spl/tests/SplFixedArray_override_getIterator.phpt [new file with mode: 0644]
ext/spl/tests/fixedarray_003.phpt [deleted file]
ext/spl/tests/fixedarray_019.phpt [deleted file]