3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Calling it supervisor because it maintains an internal state, // Receiver because it receives messages. $supervisor = makeReceiver(); // ...(243) The internal id, optional // Now we're messaging into that state. Here, we're passing commands. $supervisor->send(['addUser','amy']); $supervisor->send(['addUser','bob']); $supervisor->send(['addUser','ellen']); $supervisor->send(['addUser','joe']); $supervisor->send(['addUser','mark']); $supervisor->send(['addUser','janice']); // Nope $supervisor->send(['addUser','franke']); // Sorry, too many // This is a pointer to what's been yielded in the generator. // It is now an Iterator (next, rewind). $users = $supervisor->send(_m('iterator')); function _m($message) { return [$message, null]; } foreach($users as $user) { echo "User is `$user`.".PHP_EOL; } // This tells it to hari-kari. var_dump($supervisor->send(['SIGHUP', $supervisor])); $supervisor2 = makeReceiver(null, ['eduardo','paul','simon',]); $supervisor2->send(['addUser','ellen']); $supervisor2->send(['addUser','joe']); $users2 = $supervisor2->send(_m('iterator')); foreach($users2 as $user2) { echo "User2 is `$user2`.".PHP_EOL; } // Walk the plank, aargh! matey! var_dump($supervisor2->send(['SIGHUP', $supervisor])); function makeReceiver(?int $id = null, ?array $names = null) { $names = $names ?? []; $cNames = 0; $maxUsers = 5; $running = true; $id = $id ?? random_int(1, 500000); $log = function($signal, $message, ?string $tmpl = null) use($id) { $tmpl = $tmpl ?? 'Receiver(%d): [%s](%s)'; echo sprintf($tmpl, $id, $signal, \json_encode($message)).PHP_EOL; }; while ($running) { list($signal, $message) = yield; $log($signal, $message); switch($signal) { case 'SIGHUP': // Hang up! $running = false; return null; case 'SIGCYC': // Recycle, reset $names = []; $cNames = 0; break; case 'addUser': if ($maxUsers > $cNames) { $names[] = $message; $cNames++; } break; case 'iterator': $maxUsers = 0; yield array_values(array_unique(array_values($names))); } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 39
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 39
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 39
2 jumps found. (Code = 77) Position 1 = 67, Position 2 = 74
Branch analysis from position: 67
2 jumps found. (Code = 78) Position 1 = 68, Position 2 = 74
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
Branch analysis from position: 39
filename:       /in/gXCVc
function name:  (null)
number of ops:  84
compiled vars:  !0 = $supervisor, !1 = $users, !2 = $user, !3 = $supervisor2, !4 = $users2, !5 = $user2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   INIT_FCALL_BY_NAME                                       'makeReceiver'
          1        DO_FCALL                                      0  $6      
          2        ASSIGN                                                   !0, $6
    8     3        INIT_METHOD_CALL                                         !0, 'send'
          4        SEND_VAL_EX                                              <array>
          5        DO_FCALL                                      0          
    9     6        INIT_METHOD_CALL                                         !0, 'send'
          7        SEND_VAL_EX                                              <array>
          8        DO_FCALL                                      0          
   10     9        INIT_METHOD_CALL                                         !0, 'send'
         10        SEND_VAL_EX                                              <array>
         11        DO_FCALL                                      0          
   11    12        INIT_METHOD_CALL                                         !0, 'send'
         13        SEND_VAL_EX                                              <array>
         14        DO_FCALL                                      0          
   12    15        INIT_METHOD_CALL                                         !0, 'send'
         16        SEND_VAL_EX                                              <array>
         17        DO_FCALL                                      0          
   13    18        INIT_METHOD_CALL                                         !0, 'send'
         19        SEND_VAL_EX                                              <array>
         20        DO_FCALL                                      0          
   14    21        INIT_METHOD_CALL                                         !0, 'send'
         22        SEND_VAL_EX                                              <array>
         23        DO_FCALL                                      0          
   18    24        INIT_METHOD_CALL                                         !0, 'send'
         25        INIT_FCALL_BY_NAME                                       '_m'
         26        SEND_VAL_EX                                              'iterator'
         27        DO_FCALL                                      0  $15     
         28        SEND_VAR_NO_REF_EX                                       $15
         29        DO_FCALL                                      0  $16     
         30        ASSIGN                                                   !1, $16
   24    31      > FE_RESET_R                                       $18     !1, ->39
         32    > > FE_FETCH_R                                               $18, !2, ->39
   25    33    >   ROPE_INIT                                     3  ~20     'User+is+%60'
         34        ROPE_ADD                                      1  ~20     ~20, !2
         35        ROPE_END                                      2  ~19     ~20, '%60.'
         36        CONCAT                                           ~22     ~19, '%0A'
         37        ECHO                                                     ~22
   24    38      > JMP                                                      ->32
         39    >   FE_FREE                                                  $18
   29    40        INIT_FCALL                                               'var_dump'
         41        INIT_METHOD_CALL                                         !0, 'send'
         42        INIT_ARRAY                                       ~23     'SIGHUP'
         43        ADD_ARRAY_ELEMENT                                ~23     !0
         44        SEND_VAL_EX                                              ~23
         45        DO_FCALL                                      0  $24     
         46        SEND_VAR                                                 $24
         47        DO_ICALL                                                 
   31    48        INIT_FCALL_BY_NAME                                       'makeReceiver'
         49        SEND_VAL_EX                                              null
         50        SEND_VAL_EX                                              <array>
         51        DO_FCALL                                      0  $26     
         52        ASSIGN                                                   !3, $26
   32    53        INIT_METHOD_CALL                                         !3, 'send'
         54        SEND_VAL_EX                                              <array>
         55        DO_FCALL                                      0          
   33    56        INIT_METHOD_CALL                                         !3, 'send'
         57        SEND_VAL_EX                                              <array>
         58        DO_FCALL                                      0          
   34    59        INIT_METHOD_CALL                                         !3, 'send'
         60        INIT_FCALL                                               '_m'
         61        SEND_VAL                                                 'iterator'
         62        DO_FCALL                                      0  $30     
         63        SEND_VAR_NO_REF_EX                                       $30
         64        DO_FCALL                                      0  $31     
         65        ASSIGN                                                   !4, $31
   36    66      > FE_RESET_R                                       $33     !4, ->74
         67    > > FE_FETCH_R                                               $33, !5, ->74
   37    68    >   ROPE_INIT                                     3  ~35     'User2+is+%60'
         69        ROPE_ADD                                      1  ~35     ~35, !5
         70        ROPE_END                                      2  ~34     ~35, '%60.'
         71        CONCAT                                           ~37     ~34, '%0A'
         72        ECHO                                                     ~37
   36    73      > JMP                                                      ->67
         74    >   FE_FREE                                                  $33
   41    75        INIT_FCALL                                               'var_dump'
         76        INIT_METHOD_CALL                                         !3, 'send'
         77        INIT_ARRAY                                       ~38     'SIGHUP'
         78        ADD_ARRAY_ELEMENT                                ~38     !0
         79        SEND_VAL_EX                                              ~38
         80        DO_FCALL                                      0  $39     
         81        SEND_VAR                                                 $39
         82        DO_ICALL                                                 
   78    83      > RETURN                                                   1

Function _m:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gXCVc
function name:  _m
number of ops:  5
compiled vars:  !0 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   21     1        INIT_ARRAY                                       ~1      !0
          2        ADD_ARRAY_ELEMENT                                ~1      null
          3      > RETURN                                                   ~1
   22     4*     > RETURN                                                   null

End of function _m

Function makereceiver:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 63, Position 2 = 20
Branch analysis from position: 63
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 20
6 jumps found. (Code = 188) Position 1 = 40, Position 2 = 42, Position 3 = 45, Position 4 = 51, Position 5 = 62, Position 6 = 31
Branch analysis from position: 40
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 50
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 50
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 63, Position 2 = 20
Branch analysis from position: 63
Branch analysis from position: 20
Branch analysis from position: 62
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 40
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 42
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 37, Position 2 = 45
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 51
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 51
Branch analysis from position: 45
Branch analysis from position: 42
Branch analysis from position: 40
filename:       /in/gXCVc
function name:  makeReceiver
number of ops:  64
compiled vars:  !0 = $id, !1 = $names, !2 = $cNames, !3 = $maxUsers, !4 = $running, !5 = $log, !6 = $signal, !7 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV_INIT                                        !0      null
          1        RECV_INIT                                        !1      null
          2        GENERATOR_CREATE                                         
   44     3        COALESCE                                         ~8      !1
          4        QM_ASSIGN                                        ~8      <array>
          5        ASSIGN                                                   !1, ~8
   45     6        ASSIGN                                                   !2, 0
   46     7        ASSIGN                                                   !3, 5
   47     8        ASSIGN                                                   !4, <true>
   48     9        COALESCE                                         ~13     !0
         10        INIT_FCALL                                               'random_int'
         11        SEND_VAL                                                 1
         12        SEND_VAL                                                 500000
         13        DO_ICALL                                         $14     
         14        QM_ASSIGN                                        ~13     $14
         15        ASSIGN                                                   !0, ~13
   50    16        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgXCVc%3A50%240'
         17        BIND_LEXICAL                                             ~16, !0
         18        ASSIGN                                                   !5, ~16
   55    19      > JMP                                                      ->62
   56    20    >   YIELD                                            $18     
         21        FETCH_LIST_R                                     $19     $18, 0
         22        ASSIGN                                                   !6, $19
         23        FETCH_LIST_R                                     $21     $18, 1
         24        ASSIGN                                                   !7, $21
         25        FREE                                                     $18
   57    26        INIT_DYNAMIC_CALL                                        !5
         27        SEND_VAR_EX                                              !6
         28        SEND_VAR_EX                                              !7
         29        DO_FCALL                                      0          
   59    30      > SWITCH_STRING                                            !6, [ 'SIGHUP':->40, 'SIGCYC':->42, 'addUser':->45, 'iterator':->51, ], ->62
   60    31    >   IS_EQUAL                                                 !6, 'SIGHUP'
         32      > JMPNZ                                                    ~24, ->40
   63    33    >   IS_EQUAL                                                 !6, 'SIGCYC'
         34      > JMPNZ                                                    ~24, ->42
   67    35    >   IS_EQUAL                                                 !6, 'addUser'
         36      > JMPNZ                                                    ~24, ->45
   73    37    >   IS_EQUAL                                                 !6, 'iterator'
         38      > JMPNZ                                                    ~24, ->51
         39    > > JMP                                                      ->62
   61    40    >   ASSIGN                                                   !4, <false>
   62    41      > GENERATOR_RETURN                                         
   64    42    >   ASSIGN                                                   !1, <array>
   65    43        ASSIGN                                                   !2, 0
   66    44      > JMP                                                      ->62
   68    45    >   IS_SMALLER                                               !2, !3
         46      > JMPZ                                                     ~28, ->50
   69    47    >   ASSIGN_DIM                                               !1
         48        OP_DATA                                                  !7
   70    49        PRE_INC                                                  !2
   72    50    > > JMP                                                      ->62
   74    51    >   ASSIGN                                                   !3, 0
   75    52        INIT_FCALL                                               'array_values'
         53        INIT_FCALL                                               'array_unique'
         54        INIT_FCALL                                               'array_values'
         55        SEND_VAR                                                 !1
         56        DO_ICALL                                         $32     
         57        SEND_VAR                                                 $32
         58        DO_ICALL                                         $33     
         59        SEND_VAR                                                 $33
         60        DO_ICALL                                         $34     
         61        YIELD                                                    $34
   55    62    > > JMPNZ                                                    !4, ->20
   78    63    > > GENERATOR_RETURN                                         

End of function makereceiver

Function %00%7Bclosure%7D%2Fin%2FgXCVc%3A50%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gXCVc
function name:  {closure}
number of ops:  19
compiled vars:  !0 = $signal, !1 = $message, !2 = $tmpl, !3 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
          3        BIND_STATIC                                              !3
   51     4        COALESCE                                         ~4      !2
          5        QM_ASSIGN                                        ~4      'Receiver%28%25d%29%3A+%5B%25s%5D%28%25s%29'
          6        ASSIGN                                                   !2, ~4
   52     7        INIT_FCALL                                               'sprintf'
          8        SEND_VAR                                                 !2
          9        SEND_VAR                                                 !3
         10        SEND_VAR                                                 !0
         11        INIT_FCALL                                               'json_encode'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $6      
         14        SEND_VAR                                                 $6
         15        DO_ICALL                                         $7      
         16        CONCAT                                           ~8      $7, '%0A'
         17        ECHO                                                     ~8
   53    18      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FgXCVc%3A50%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
135.33 ms | 1419 KiB | 26 Q