3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* A tasks job is to count from 0 to $limit */ class Task { public function __construct($limit) { $this->limit = ($limit + 1) * 100; } public function doWork($async) { if ($async) { /* do a little work and allow another task to continue */ $this->position++; } else { /* do all the work */ for (; $this->position < $this->limit; $this->position++); } } public function hasWork() { return $this->position <= $this->limit; } public function getPosition() { return $this->position; } private $position = 0; private $limit = 0; } /* helper for detecting completion */ function finished($tasks) { foreach ($tasks as $task) { if ($task->hasWork()) return false; } return true; } /* helper for printing positions */ function positions($tasks, $numeric = false) { $positions = array(); foreach ($tasks as $task) { $positions[] = $numeric || $task->hasWork() ? $task->getPosition() : "-"; } return $positions; } /* create three tasks */ $tasks = array( new Task(0), new Task(1), new Task(2) ); /* execute tasks asynchronously */ while (!finished($tasks)) { foreach ($tasks as $task) { if ($task->hasWork()) { $task->doWork(true); } vprintf( "% 4s % 4s % 4s\n", positions($tasks)); } } printf("------------------\n"); $tasks = array( new Task(0), new Task(1), new Task(2) ); /* execute tasks synchronously */ foreach ($tasks as $task) { if ($task->hasWork()) { $task->doWork(false); } vprintf( "% 4s % 4s % 4s\n", positions($tasks)); } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 14
Branch analysis from position: 36
2 jumps found. (Code = 77) Position 1 = 53, Position 2 = 68
Branch analysis from position: 53
2 jumps found. (Code = 78) Position 1 = 54, Position 2 = 68
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 60
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
Branch analysis from position: 60
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 30
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 30
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 22
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 14
Branch analysis from position: 36
Branch analysis from position: 14
Branch analysis from position: 30
filename:       /in/FpNRu
function name:  (null)
number of ops:  70
compiled vars:  !0 = $tasks, !1 = $task
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   NEW                                              $2      'Task'
          1        SEND_VAL_EX                                              0
          2        DO_FCALL                                      0          
          3        INIT_ARRAY                                       ~4      $2
   50     4        NEW                                              $5      'Task'
          5        SEND_VAL_EX                                              1
          6        DO_FCALL                                      0          
          7        ADD_ARRAY_ELEMENT                                ~4      $5
   51     8        NEW                                              $7      'Task'
          9        SEND_VAL_EX                                              2
         10        DO_FCALL                                      0          
         11        ADD_ARRAY_ELEMENT                                ~4      $7
   48    12        ASSIGN                                                   !0, ~4
   55    13      > JMP                                                      ->31
   56    14    > > FE_RESET_R                                       $10     !0, ->30
         15    > > FE_FETCH_R                                               $10, !1, ->30
   57    16    >   INIT_METHOD_CALL                                         !1, 'hasWork'
         17        DO_FCALL                                      0  $11     
         18      > JMPZ                                                     $11, ->22
   58    19    >   INIT_METHOD_CALL                                         !1, 'doWork'
         20        SEND_VAL_EX                                              <true>
         21        DO_FCALL                                      0          
   60    22    >   INIT_FCALL                                               'vprintf'
         23        SEND_VAL                                                 '%25+4s+%25+4s+%25+4s%0A'
         24        INIT_FCALL                                               'positions'
         25        SEND_VAR                                                 !0
         26        DO_FCALL                                      0  $13     
         27        SEND_VAR                                                 $13
         28        DO_ICALL                                                 
   56    29      > JMP                                                      ->15
         30    >   FE_FREE                                                  $10
   55    31    >   INIT_FCALL                                               'finished'
         32        SEND_VAR                                                 !0
         33        DO_FCALL                                      0  $15     
         34        BOOL_NOT                                         ~16     $15
         35      > JMPNZ                                                    ~16, ->14
   64    36    >   INIT_FCALL                                               'printf'
         37        SEND_VAL                                                 '------------------%0A'
         38        DO_ICALL                                                 
   67    39        NEW                                              $18     'Task'
         40        SEND_VAL_EX                                              0
         41        DO_FCALL                                      0          
         42        INIT_ARRAY                                       ~20     $18
   68    43        NEW                                              $21     'Task'
         44        SEND_VAL_EX                                              1
         45        DO_FCALL                                      0          
         46        ADD_ARRAY_ELEMENT                                ~20     $21
   69    47        NEW                                              $23     'Task'
         48        SEND_VAL_EX                                              2
         49        DO_FCALL                                      0          
         50        ADD_ARRAY_ELEMENT                                ~20     $23
   66    51        ASSIGN                                                   !0, ~20
   73    52      > FE_RESET_R                                       $26     !0, ->68
         53    > > FE_FETCH_R                                               $26, !1, ->68
   74    54    >   INIT_METHOD_CALL                                         !1, 'hasWork'
         55        DO_FCALL                                      0  $27     
         56      > JMPZ                                                     $27, ->60
   75    57    >   INIT_METHOD_CALL                                         !1, 'doWork'
         58        SEND_VAL_EX                                              <false>
         59        DO_FCALL                                      0          
   77    60    >   INIT_FCALL                                               'vprintf'
         61        SEND_VAL                                                 '%25+4s+%25+4s+%25+4s%0A'
         62        INIT_FCALL                                               'positions'
         63        SEND_VAR                                                 !0
         64        DO_FCALL                                      0  $29     
         65        SEND_VAR                                                 $29
         66        DO_ICALL                                                 
   73    67      > JMP                                                      ->53
         68    >   FE_FREE                                                  $26
   79    69      > RETURN                                                   1

Function finished:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 9
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/FpNRu
function name:  finished
number of ops:  12
compiled vars:  !0 = $tasks, !1 = $task
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1      > FE_RESET_R                                       $2      !0, ->9
          2    > > FE_FETCH_R                                               $2, !1, ->9
   29     3    >   INIT_METHOD_CALL                                         !1, 'hasWork'
          4        DO_FCALL                                      0  $3      
          5      > JMPZ                                                     $3, ->8
   30     6    >   FE_FREE                                                  $2
          7      > RETURN                                                   <false>
   28     8    > > JMP                                                      ->2
          9    >   FE_FREE                                                  $2
   33    10      > RETURN                                                   <true>
   34    11*     > RETURN                                                   null

End of function finished

Function positions:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 18
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 18
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/FpNRu
function name:  positions
number of ops:  21
compiled vars:  !0 = $tasks, !1 = $numeric, !2 = $positions, !3 = $task
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   38     2        ASSIGN                                                   !2, <array>
   39     3      > FE_RESET_R                                       $5      !0, ->18
          4    > > FE_FETCH_R                                               $5, !3, ->18
   41     5    > > JMPNZ_EX                                         ~7      !1, ->9
          6    >   INIT_METHOD_CALL                                         !3, 'hasWork'
          7        DO_FCALL                                      0  $8      
          8        BOOL                                             ~7      $8
          9    > > JMPZ                                                     ~7, ->14
   42    10    >   INIT_METHOD_CALL                                         !3, 'getPosition'
         11        DO_FCALL                                      0  $9      
         12        QM_ASSIGN                                        ~10     $9
         13      > JMP                                                      ->15
         14    >   QM_ASSIGN                                        ~10     '-'
   40    15    >   ASSIGN_DIM                                               !2
   42    16        OP_DATA                                                  ~10
   39    17      > JMP                                                      ->4
         18    >   FE_FREE                                                  $5
   44    19      > RETURN                                                   !2
   45    20*     > RETURN                                                   null

End of function positions

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

End of function __construct

Function dowork:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 4
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 5
Branch analysis from position: 10
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 5
Branch analysis from position: 10
Branch analysis from position: 5
filename:       /in/FpNRu
function name:  doWork
number of ops:  11
compiled vars:  !0 = $async
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1      > JMPZ                                                     !0, ->4
   12     2    >   PRE_INC_OBJ                                              'position'
          3      > JMP                                                      ->10
   15     4    > > JMP                                                      ->6
          5    >   PRE_INC_OBJ                                              'position'
          6    >   FETCH_OBJ_R                                      ~3      'position'
          7        FETCH_OBJ_R                                      ~4      'limit'
          8        IS_SMALLER                                               ~3, ~4
          9      > JMPNZ                                                    ~5, ->5
   17    10    > > RETURN                                                   null

End of function dowork

Function haswork:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FpNRu
function name:  hasWork
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   FETCH_OBJ_R                                      ~0      'position'
          1        FETCH_OBJ_R                                      ~1      'limit'
          2        IS_SMALLER_OR_EQUAL                              ~2      ~0, ~1
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function haswork

Function getposition:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FpNRu
function name:  getPosition
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   FETCH_OBJ_R                                      ~0      'position'
          1      > RETURN                                                   ~0
          2*     > RETURN                                                   null

End of function getposition

End of class Task.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.13 ms | 1415 KiB | 20 Q