I'm building a custom collection class called Plans.
<?php
namespace App\Plans;
use Illuminate\Support\Collection;
class Plans extends Collection
{
public function withStripeId(string $stripeId) {
...
}
}
Plans has a few custom methods (like retrieving by stripe id) and should contain an object of each of my Plan classes.
It should be retrievable from the service container with the Plan objects already in it.
Illuminate\Support\Collection has an $items property but I can't instantiate the Plan objects there.
I didn't want to override the constructor because what happens when the constructor on the underlying class changes? I'd always have to check that class to see if something changed.
Then I remembered I could just call the constructor by myself like this:
public function __construct($items = [])
{
$plans = [
new ProPlan(),
new AgencyPlan()
];
parent::__construct($plans);
}