3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); class Game { public function __construct( public readonly string $name ) {} } class GameCollection { private array $games = []; public function add(Game $game): void { $this->games[$game->name] = $game; } public function remove(Game $game): void { unset($this->games[$game->name]); } public function getAll(): array { return $this->games; } } class GameStore { public function __construct( private GameCollection $games, private GameCollection $loans ) {} public function addGame(Game $game): void { $this->games->add($game); } public function getAllGames(): array { return $this->games->getAll(); } public function checkOut(Game $game): void { $this->loans->add($game); $this->games->remove($game); } public function checkIn(Game $game): void { $this->loans->remove($game); $this->games->add($game); } public function getAllOnLoan(): array { return $this->loans->getAll(); } } $csgo = new Game('CS:GO'); $exp33 = new Game('Expedition 33'); $marioBros = new Game('Super Mario Bros'); $collection = new GameCollection(); $collection->add($csgo); $collection->add($exp33); $collection->add($marioBros); var_dump( 'Current collection:', $collection->getAll() ); $collection->remove($exp33); var_dump( 'Collection without Expedition 33:', $collection->getAll() ); $store = new GameStore( games: new GameCollection(), loans: new GameCollection() ); $store->addGame($csgo); $store->addGame($exp33); var_dump( 'GameStore', 'Available:', $store->getAllGames(), 'On loan:', $store->getAllOnLoan() ); $store->checkOut($csgo); var_dump( 'GameStore - checkOut', 'Available:', $store->getAllGames(), 'On loan:', $store->getAllOnLoan() ); $store->checkOut($exp33); $store->checkIn($csgo); var_dump( 'GameStore - checkIn & checkOut', 'Available:', $store->getAllGames(), 'On loan:', $store->getAllOnLoan() );
Output for 8.4.18
string(19) "Current collection:" array(3) { ["CS:GO"]=> object(Game)#1 (1) { ["name"]=> string(5) "CS:GO" } ["Expedition 33"]=> object(Game)#2 (1) { ["name"]=> string(13) "Expedition 33" } ["Super Mario Bros"]=> object(Game)#3 (1) { ["name"]=> string(16) "Super Mario Bros" } } string(33) "Collection without Expedition 33:" array(2) { ["CS:GO"]=> object(Game)#1 (1) { ["name"]=> string(5) "CS:GO" } ["Super Mario Bros"]=> object(Game)#3 (1) { ["name"]=> string(16) "Super Mario Bros" } } string(9) "GameStore" string(10) "Available:" array(2) { ["CS:GO"]=> object(Game)#1 (1) { ["name"]=> string(5) "CS:GO" } ["Expedition 33"]=> object(Game)#2 (1) { ["name"]=> string(13) "Expedition 33" } } string(8) "On loan:" array(0) { } string(20) "GameStore - checkOut" string(10) "Available:" array(1) { ["Expedition 33"]=> object(Game)#2 (1) { ["name"]=> string(13) "Expedition 33" } } string(8) "On loan:" array(1) { ["CS:GO"]=> object(Game)#1 (1) { ["name"]=> string(5) "CS:GO" } } string(30) "GameStore - checkIn & checkOut" string(10) "Available:" array(1) { ["CS:GO"]=> object(Game)#1 (1) { ["name"]=> string(5) "CS:GO" } } string(8) "On loan:" array(1) { ["Expedition 33"]=> object(Game)#2 (1) { ["name"]=> string(13) "Expedition 33" } }

preferences:
43.67 ms | 488 KiB | 5 Q