A shared room, a copied box
An object hands out its array property, and the caller changes it.
<?phpclass Cart{ private array $items = ['book']; public function items(): array { return $this->items; } public function add(string $item): void { $this->items[] = $item; }}$cart = new Cart();$list = $cart->items();$list[] = 'mug';echo count($cart->items()), PHP_EOL;$cart->add('pen');echo implode(',', $cart->items()), PHP_EOL;echo implode(',', $list), PHP_EOL;1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…