This is our Bag class — and yes, we absolutely love its simplicity 🥰
class Bag
{
public function __construct(public array $items = []) {}
}
HOW TO WORK WITH IT?
The long way (a.k.a. “why make it simple?”)
- Create collection objects
- Insert them into another collection
- Fill them with items
$bagA = new Bag();
$bagB = new Bag();
$preparedObjects = [];
$preparedObjects[] = $bagA;
$preparedObjects[] = $bagB;
$bagA->items[] = 'Bag A item 1';
$bagA->items[] = 'Bag A item 2';
$bagB->items[] = 'Bag B item 1';
$bagB->items[] = 'Bag B item 2';
The simpler way (clean, elegant, instant 😌)
- Create and insert objects right away
- Then just fill them
$preparedObjects = [
$bagA = new Bag(),
$bagB = new Bag(),
];
$bagA->items[] = 'Bag A item 1';
$bagA->items[] = 'Bag A item 2';
$bagB->items[] = 'Bag B item 1';
$bagB->items[] = 'Bag B item 2';
Top comments (0)