3v4l.org

run code in 300+ PHP versions simultaneously
<?php function tryMultiple(callable $func, int $count=3, array $acceptable=['\Throwable']) { $success = false; for($i=0; $i<$count; $i++) { try { $v = $func(); $success = true; break; } catch(\Throwable $t) { $is_ok = false; foreach($acceptable as $accept) { if( is_a($t, $accept) ) { $is_ok = true; } } if( ! $is_ok ) { throw $t; } } } if($success) { return $v; } else { throw new \Exception("Failure count exceeded limit of $count.", 42, $t); } } $always_throws = function(){throw new \Exception('bork');}; $always_works = function(){return 'work';}; var_dump(tryMultiple($always_works)); try { tryMultiple($always_throws); } catch(Exception $e) { var_dump($e->getMessage()); } try { tryMultiple($always_throws, 3, []); } catch(Exception $e) { var_dump($e->getMessage()); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 14
Branch analysis from position: 14
2 jumps found. (Code = 107) Position 1 = 15, Position 2 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Found catch point at position: 26
Branch analysis from position: 26
2 jumps found. (Code = 107) Position 1 = 27, Position 2 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Wdcvf
function name:  (null)
number of ops:  33
compiled vars:  !0 = $always_throws, !1 = $always_works, !2 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FWdcvf%3A30%240'
          1        ASSIGN                                                   !0, ~3
   31     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FWdcvf%3A31%241'
          3        ASSIGN                                                   !1, ~5
   33     4        INIT_FCALL                                               'var_dump'
          5        INIT_FCALL                                               'trymultiple'
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $7      
          8        SEND_VAR                                                 $7
          9        DO_ICALL                                                 
   34    10        INIT_FCALL                                               'trymultiple'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0          
         13      > JMP                                                      ->20
         14  E > > CATCH                                       last         'Exception'
         15    >   INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         !2, 'getMessage'
         17        DO_FCALL                                      0  $10     
         18        SEND_VAR                                                 $10
         19        DO_ICALL                                                 
   35    20    >   INIT_FCALL                                               'trymultiple'
         21        SEND_VAR                                                 !0
         22        SEND_VAL                                                 3
         23        SEND_VAL                                                 <array>
         24        DO_FCALL                                      0          
         25      > JMP                                                      ->32
         26  E > > CATCH                                       last         'Exception'
         27    >   INIT_FCALL                                               'var_dump'
         28        INIT_METHOD_CALL                                         !2, 'getMessage'
         29        DO_FCALL                                      0  $13     
         30        SEND_VAR                                                 $13
         31        DO_ICALL                                                 
         32    > > RETURN                                                   1

Function trymultiple:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 6
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 33
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Found catch point at position: 12
Branch analysis from position: 12
2 jumps found. (Code = 107) Position 1 = 13, Position 2 = -2
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 23
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 22
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 27
Branch analysis from position: 26
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 6
Branch analysis from position: 30
Branch analysis from position: 6
Branch analysis from position: 23
filename:       /in/Wdcvf
function name:  tryMultiple
number of ops:  43
compiled vars:  !0 = $func, !1 = $count, !2 = $acceptable, !3 = $success, !4 = $i, !5 = $v, !6 = $t, !7 = $is_ok, !8 = $accept
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      3
          2        RECV_INIT                                        !2      <array>
    4     3        ASSIGN                                                   !3, <false>
    5     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->28
    7     6    >   INIT_DYNAMIC_CALL                                        !0
          7        DO_FCALL                                      0  $11     
          8        ASSIGN                                                   !5, $11
    8     9        ASSIGN                                                   !3, <true>
    9    10      > JMP                                                      ->30
         11*       JMP                                                      ->27
   10    12  E > > CATCH                                       last         'Throwable'
   11    13    >   ASSIGN                                                   !7, <false>
   12    14      > FE_RESET_R                                       $15     !2, ->23
         15    > > FE_FETCH_R                                               $15, !8, ->23
   13    16    >   INIT_FCALL                                               'is_a'
         17        SEND_VAR                                                 !6
         18        SEND_VAR                                                 !8
         19        DO_ICALL                                         $16     
         20      > JMPZ                                                     $16, ->22
   14    21    >   ASSIGN                                                   !7, <true>
   12    22    > > JMP                                                      ->15
         23    >   FE_FREE                                                  $15
   17    24        BOOL_NOT                                         ~18     !7
         25      > JMPZ                                                     ~18, ->27
   18    26    > > THROW                                         0          !6
    5    27    >   PRE_INC                                                  !4
         28    >   IS_SMALLER                                               !4, !1
         29      > JMPNZ                                                    ~20, ->6
   23    30    > > JMPZ                                                     !3, ->33
   24    31    > > RETURN                                                   !5
         32*       JMP                                                      ->42
   26    33    >   NEW                                              $21     'Exception'
         34        ROPE_INIT                                     3  ~23     'Failure+count+exceeded+limit+of+'
         35        ROPE_ADD                                      1  ~23     ~23, !1
         36        ROPE_END                                      2  ~22     ~23, '.'
         37        SEND_VAL_EX                                              ~22
         38        SEND_VAL_EX                                              42
         39        SEND_VAR_EX                                              !6
         40        DO_FCALL                                      0          
         41      > THROW                                         0          $21
   28    42*     > RETURN                                                   null

End of function trymultiple

Function %00%7Bclosure%7D%2Fin%2FWdcvf%3A30%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/Wdcvf
function name:  {closure}
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   NEW                                              $0      'Exception'
          1        SEND_VAL_EX                                              'bork'
          2        DO_FCALL                                      0          
          3      > THROW                                         0          $0
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FWdcvf%3A30%240

Function %00%7Bclosure%7D%2Fin%2FWdcvf%3A31%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Wdcvf
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E > > RETURN                                                   'work'
          1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FWdcvf%3A31%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.17 ms | 1407 KiB | 20 Q