3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); class Game { public function __construct( public readonly string $name ) {} } class User { 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 { # array<User->name, GameCollection> private array $loans; public function __construct( private GameCollection $games ) {} public function addGame(Game $game): void { $this->games->add($game); } public function getAllGames(): array { return $this->games->getAll(); } public function registerUser(User $user): void { $this->loans[$user->name] = new GameCollection(); } public function checkOut(Game $game, User $user): void { $this->loans[$user->name]->add($game); $this->games->remove($game); } public function checkIn(Game $game, User $user): void { $this->loans[$user->name]->remove($game); $this->games->add($game); } public function getAllOnLoan(): array { return array_map(fn($user) => $user->getAll(), $this->loans); } } $csgo = new Game('CS:GO'); $exp33 = new Game('Expedition 33'); $marioBros = new Game('Super Mario Bros'); $johnny = new User('Johnny'); $daniel = new User('Daniel'); $store = new GameStore(games: new GameCollection()); $store->addGame($csgo); $store->addGame($exp33); $store->addGame($marioBros); $store->registerUser($daniel); $store->checkOut($exp33, $daniel); var_dump( 'Daniel registers and checksOut Expedition 33', 'Store Available Games', $store->getAllGames(), 'On Loan', $store->getAllOnLoan() ); $store->registerUser($johnny); $store->checkIn($exp33, $daniel); $store->checkOut($exp33, $johnny); var_dump( 'Daniel returns Expedition 33 and Johnny checksOut the same game', 'Store Available Games', $store->getAllGames(), 'On Loan', $store->getAllOnLoan() );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  (null)
number of ops:  77
compiled vars:  !0 = $csgo, !1 = $exp33, !2 = $marioBros, !3 = $johnny, !4 = $daniel, !5 = $store
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   68     0  E >   NEW                                                  $6      'Game'
          1        SEND_VAL_EX                                                  'CS%3AGO'
          2        DO_FCALL                                          0          
          3        ASSIGN                                                       !0, $6
   69     4        NEW                                                  $9      'Game'
          5        SEND_VAL_EX                                                  'Expedition+33'
          6        DO_FCALL                                          0          
          7        ASSIGN                                                       !1, $9
   70     8        NEW                                                  $12     'Game'
          9        SEND_VAL_EX                                                  'Super+Mario+Bros'
         10        DO_FCALL                                          0          
         11        ASSIGN                                                       !2, $12
   72    12        NEW                                                  $15     'User'
         13        SEND_VAL_EX                                                  'Johnny'
         14        DO_FCALL                                          0          
         15        ASSIGN                                                       !3, $15
   73    16        NEW                                                  $18     'User'
         17        SEND_VAL_EX                                                  'Daniel'
         18        DO_FCALL                                          0          
         19        ASSIGN                                                       !4, $18
   75    20        NEW                                                  $21     'GameStore'
         21        NEW                                                  $22     'GameCollection'
         22        DO_FCALL                                          0          
         23        SEND_VAR_NO_REF_EX                                           $22, 'games'
         24        CHECK_UNDEF_ARGS                                             
         25        DO_FCALL                                          1          
         26        ASSIGN                                                       !5, $21
   77    27        INIT_METHOD_CALL                                             !5, 'addGame'
         28        SEND_VAR_EX                                                  !0
         29        DO_FCALL                                          0          
   78    30        INIT_METHOD_CALL                                             !5, 'addGame'
         31        SEND_VAR_EX                                                  !1
         32        DO_FCALL                                          0          
   79    33        INIT_METHOD_CALL                                             !5, 'addGame'
         34        SEND_VAR_EX                                                  !2
         35        DO_FCALL                                          0          
   81    36        INIT_METHOD_CALL                                             !5, 'registerUser'
         37        SEND_VAR_EX                                                  !4
         38        DO_FCALL                                          0          
   82    39        INIT_METHOD_CALL                                             !5, 'checkOut'
         40        SEND_VAR_EX                                                  !1
         41        SEND_VAR_EX                                                  !4
         42        DO_FCALL                                          0          
   84    43        INIT_FCALL                                                   'var_dump'
   85    44        SEND_VAL                                                     'Daniel+registers+and+checksOut+Expedition+33'
   86    45        SEND_VAL                                                     'Store+Available+Games'
   87    46        INIT_METHOD_CALL                                             !5, 'getAllGames'
         47        DO_FCALL                                          0  $31     
         48        SEND_VAR                                                     $31
   88    49        SEND_VAL                                                     'On+Loan'
   89    50        INIT_METHOD_CALL                                             !5, 'getAllOnLoan'
         51        DO_FCALL                                          0  $32     
         52        SEND_VAR                                                     $32
   84    53        DO_ICALL                                                     
   92    54        INIT_METHOD_CALL                                             !5, 'registerUser'
         55        SEND_VAR_EX                                                  !3
         56        DO_FCALL                                          0          
   93    57        INIT_METHOD_CALL                                             !5, 'checkIn'
         58        SEND_VAR_EX                                                  !1
         59        SEND_VAR_EX                                                  !4
         60        DO_FCALL                                          0          
   94    61        INIT_METHOD_CALL                                             !5, 'checkOut'
         62        SEND_VAR_EX                                                  !1
         63        SEND_VAR_EX                                                  !3
         64        DO_FCALL                                          0          
   96    65        INIT_FCALL                                                   'var_dump'
   97    66        SEND_VAL                                                     'Daniel+returns+Expedition+33+and+Johnny+checksOut+the+same+game'
   98    67        SEND_VAL                                                     'Store+Available+Games'
   99    68        INIT_METHOD_CALL                                             !5, 'getAllGames'
         69        DO_FCALL                                          0  $37     
         70        SEND_VAR                                                     $37
  100    71        SEND_VAL                                                     'On+Loan'
  101    72        INIT_METHOD_CALL                                             !5, 'getAllOnLoan'
         73        DO_FCALL                                          0  $38     
         74        SEND_VAR                                                     $38
   96    75        DO_ICALL                                                     
  102    76      > RETURN                                                       1

Class Game:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
          1        ASSIGN_OBJ                                                   'name'
          2        OP_DATA                                                      !0
    8     3      > RETURN                                                       null

End of function __construct

End of class Game.

Class User:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   13     0  E >   RECV                                                 !0      
          1        ASSIGN_OBJ                                                   'name'
          2        OP_DATA                                                      !0
   14     3      > RETURN                                                       null

End of function __construct

End of class User.

Class GameCollection:
Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  add
number of ops:  6
compiled vars:  !0 = $game
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   20     0  E >   RECV                                                 !0      
   21     1        FETCH_OBJ_R                                          ~2      !0, 'name'
          2        FETCH_OBJ_W                                          $1      'games'
          3        ASSIGN_DIM                                                   $1, ~2
          4        OP_DATA                                                      !0
   22     5      > RETURN                                                       null

End of function add

Function remove:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  remove
number of ops:  5
compiled vars:  !0 = $game
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     0  E >   RECV                                                 !0      
   25     1        FETCH_OBJ_R                                          ~2      !0, 'name'
          2        FETCH_OBJ_UNSET                                      $1      'games'
          3        UNSET_DIM                                                    $1, ~2
   26     4      > RETURN                                                       null

End of function remove

Function getall:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  getAll
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   29     0  E >   FETCH_OBJ_R                                          ~0      'games'
          1        VERIFY_RETURN_TYPE                                           ~0
          2      > RETURN                                                       ~0
   30     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function getall

End of class GameCollection.

Class GameStore:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  __construct
number of ops:  4
compiled vars:  !0 = $games
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   38     0  E >   RECV                                                 !0      
          1        ASSIGN_OBJ                                                   'games'
          2        OP_DATA                                                      !0
   39     3      > RETURN                                                       null

End of function __construct

Function addgame:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  addGame
number of ops:  6
compiled vars:  !0 = $game
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   RECV                                                 !0      
   42     1        FETCH_OBJ_R                                          ~1      'games'
          2        INIT_METHOD_CALL                                             ~1, 'add'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0          
   43     5      > RETURN                                                       null

End of function addgame

Function getallgames:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  getAllGames
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   46     0  E >   FETCH_OBJ_R                                          ~0      'games'
          1        INIT_METHOD_CALL                                             ~0, 'getAll'
          2        DO_FCALL                                          0  $1      
          3        VERIFY_RETURN_TYPE                                           $1
          4      > RETURN                                                       $1
   47     5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function getallgames

Function registeruser:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  registerUser
number of ops:  8
compiled vars:  !0 = $user
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   49     0  E >   RECV                                                 !0      
   50     1        FETCH_OBJ_R                                          ~2      !0, 'name'
          2        NEW                                                  $4      'GameCollection'
          3        DO_FCALL                                          0          
          4        FETCH_OBJ_W                                          $1      'loans'
          5        ASSIGN_DIM                                                   $1, ~2
          6        OP_DATA                                                      $4
   51     7      > RETURN                                                       null

End of function registeruser

Function checkout:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  checkOut
number of ops:  13
compiled vars:  !0 = $game, !1 = $user
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   53     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   54     2        FETCH_OBJ_R                                          ~3      !1, 'name'
          3        FETCH_OBJ_R                                          ~2      'loans'
          4        FETCH_DIM_R                                          ~4      ~2, ~3
          5        INIT_METHOD_CALL                                             ~4, 'add'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
   55     8        FETCH_OBJ_R                                          ~6      'games'
          9        INIT_METHOD_CALL                                             ~6, 'remove'
         10        SEND_VAR_EX                                                  !0
         11        DO_FCALL                                          0          
   56    12      > RETURN                                                       null

End of function checkout

Function checkin:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  checkIn
number of ops:  13
compiled vars:  !0 = $game, !1 = $user
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   58     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   59     2        FETCH_OBJ_R                                          ~3      !1, 'name'
          3        FETCH_OBJ_R                                          ~2      'loans'
          4        FETCH_DIM_R                                          ~4      ~2, ~3
          5        INIT_METHOD_CALL                                             ~4, 'remove'
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
   60     8        FETCH_OBJ_R                                          ~6      'games'
          9        INIT_METHOD_CALL                                             ~6, 'add'
         10        SEND_VAR_EX                                                  !0
         11        DO_FCALL                                          0          
   61    12      > RETURN                                                       null

End of function checkin

Function getallonloan:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  getAllOnLoan
number of ops:  10
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   64     0  E >   INIT_FCALL                                                   'array_map'
          1        DECLARE_LAMBDA_FUNCTION                              ~0      [0]
          2        SEND_VAL                                                     ~0
          3        FETCH_OBJ_R                                          ~1      'loans'
          4        SEND_VAL                                                     ~1
          5        DO_ICALL                                             $2      
          6        VERIFY_RETURN_TYPE                                           $2
          7      > RETURN                                                       $2
   65     8*       VERIFY_RETURN_TYPE                                           
          9*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/atLZu
function name:  {closure:GameStore::getAllOnLoan():64}
number of ops:  5
compiled vars:  !0 = $user
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   64     0  E >   RECV                                                 !0      
          1        INIT_METHOD_CALL                                             !0, 'getAll'
          2        DO_FCALL                                          0  $1      
          3      > RETURN                                                       $1
          4*     > RETURN                                                       null

End of Dynamic Function 0

End of function getallonloan

End of class GameStore.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
158.74 ms | 1115 KiB | 15 Q