# Extended collection class not behaving the same as base collection class

**URL:** <https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734>\
**Category:** Need Help\
**Created:** [December 11, 2023, 5:40pm UTC](https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734 "2023-12-11T17:40:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 11, 2023, 5:40pm UTC](https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734/1 "2023-12-11T17:40:51Z")

</div>

If you put this code into `tests/TestCase/CollectionTest.php`, the test will fail.

```auto
<?php
namespace App\Test\TestCase;

use Cake\Collection\Collection;
use PHPUnit\Framework\TestCase;

class MyCollection extends Collection { }

class CollectionTest extends TestCase
{
    public function testIterators(): void {
        $records = [
            ['field' => true],
            ['field' => false],
            ['field' => true],
        ];

        $collection = new MyCollection($records);
        $count = 0;
        foreach ($collection as $investment) {
            $count ++;
            if ($count > 20) {
                break;
            }
            if ($collection->every(fn ($record) => $record['field'] === true)) {
                // Nothing, really, just need to execute the every() call
            }
        }

        $this->assertEquals(3, $count);
    }
}

```

But if you change `new MyCollection` to `new Collection`, it works just fine. I can’t figure why this is the case, when the `MyCollection` class here is literally nothing different from the base class. (Of course, in my actual use case, it has a few additional functions, but this is the minimum reproducible example.)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 11, 2023, 7:42pm UTC](https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734/2 "2023-12-11T19:42:09Z")

</div>

Seems like a bug inside the CollectionTrait.  
The line which causes the problem is this one:

> <https://github.com/cakephp/cakephp/blob/5.x/src/Collection/CollectionTrait.php#L937>

I’ll create an issue in the repo since this collection magic is out of my league…

> <https://github.com/cakephp/cakephp/issues/17483>
>
> \### Description
> 
> Original Thread: https://discourse.cakephp.org/t/extended-colle…ction-class-not-behaving-the-same-as-base-collection-class/11734
> 
> As can be seen in the mentioned forum link (thanks @Zuluru) if one extends a collection it behaves differently (in this specific case it causes an infinite loop)
> 
> I'll just copy/paste the example in here:
> 
> \`\`\`
> \<?php
> namespace App\\Test\\TestCase;
> 
> use Cake\\Collection\\Collection;
> use PHPUnit\\Framework\\TestCase;
> 
> class MyCollection extends Collection { }
> 
> class CollectionTest extends TestCase
> {
> public function testIterators(): void {
> $records = \[
> \['field' =\> true\],
> \['field' =\> false\],
> \['field' =\> true\],
> \];
> 
> $collection = new MyCollection($records);
> $count = 0;
> foreach ($collection as $investment) {
> $count ++;
> if ($count \> 20) {
> break;
> }
> if ($collection-\>every(fn ($record) =\> $record\['field'\] === true)) {
> // Nothing, really, just need to execute the every() call
> }
> }
> 
> $this-\>assertEquals(3, $count);
> }
> }
> \`\`\`
> 
> if you change \`new MyCollection\` to \`new Collection\` it works just fine.
> 
> My guess is related to this line: https://github.com/cakephp/cakephp/blob/5.x/src/Collection/CollectionTrait.php#L937
> 
> But I don't use collections and have not yet doven into this black magic.
> 
> \### CakePHP Version
> 
> 5.0.3
> 
> \### PHP Version
> 
> 8.2

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 11, 2023, 8:35pm UTC](https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734/3 "2023-12-11T20:35:13Z")

</div>

Ah, that should maybe also be an `instanceof`.

edit: Confirmed that changing that line to `$iterator instanceof Collection` lets this particular test pass. Haven’t tried running the Cake test suite with that change; it may break other things?

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 12, 2023, 7:12am UTC](https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734/4 "2023-12-12T07:12:39Z")

</div>

It does break other things, thats why I created an issue, not a PR
