Custom collection class sorting on construct

Hi everyone! I created a custom class that extend the Collection class and so far so good. I’ve been using the inherited methods and added others. I can do sort() after creating the object but I would like to do it automatically when creating it I don’t have a clue where where I should look to try to do this so any help is very welcome.

Thanks in advance.

Which Collection class?

hmm I’m not sure what you asked. Probably I’m using the wrong terminolgy, sorry.

I’m extending this: Collection | Kirby CMS
Creating a class in my plugin myClass.php with:

<?php
namespace guidoferreyra\myPlugin;

use Kirby\Toolkit\Collection;

class myCollection extends Collection
{
   ...
}

Thanks

Ok, so you are extending the Kirby\Toolkit\Collection class (What you linked to is the Kirby\Cms\Collection class, which extends the Kirby\Toolkit\Collection class. And then there is also the Kirby\Api\Collection class, that’s why I asked.

In the constructor, you could sort the data array before passing it to $this->set():

 public function __construct(array $data = [], bool $caseSensitive = false)
    {
        $this->caseSensitive = $caseSensitive;
        $this->set($data);
    }
1 Like

Thanks I will try this! Also thanks for the explanation I was confused about all Collection classes. Now I know better what to look

As usual you saved my life.