3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace e { const string = 'string'; const int = 'int'; const float = 'float'; const bool = 'boolean'; /** * @template T * @param typename<T> $type * @return T */ function check($var, string $type) { if (\gettype($var) === $type) { return $var; } $check = \sprintf("is_", $type); if (\function_exists($check) && $check($var)) { return $var; } elseif (\is_a($var, $type)) { return $var; } throw new \Exception(\sprintf('Expected %s, got %s', $type, gettype($var))); } // hack : // function as<T>(mixed $var, typename<T> $type): T { // } } namespace { try { $var = 'string'; $var = e\check($var, e\int); } catch(\Exception $e) { echo $e->getMessage(), "\n"; } $var = false; $var = e\check($var, e\bool); \var_dump($var); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6B3hA
function name:  (null)
number of ops:  28
compiled vars:  !0 = $var, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   DECLARE_CONST                                            'e%5Cstring', 'string'
    5     1        DECLARE_CONST                                            'e%5Cint', 'int'
    6     2        DECLARE_CONST                                            'e%5Cfloat', 'float'
    7     3        DECLARE_CONST                                            'e%5Cbool', 'boolean'
   39     4        ASSIGN                                                   !0, 'string'
   40     5        INIT_FCALL                                               'e%5Ccheck'
          6        SEND_VAR                                                 !0
          7        FETCH_CONSTANT                                   ~3      'e%5Cint'
          8        SEND_VAL                                                 ~3
          9        DO_FCALL                                      0  $4      
         10        ASSIGN                                                   !0, $4
         11      > JMP                                                      ->17
   41    12  E > > CATCH                                       last         'Exception'
   42    13    >   INIT_METHOD_CALL                                         !1, 'getMessage'
         14        DO_FCALL                                      0  $6      
         15        ECHO                                                     $6
         16        ECHO                                                     '%0A'
   45    17    >   ASSIGN                                                   !0, <false>
   46    18        INIT_FCALL                                               'e%5Ccheck'
         19        SEND_VAR                                                 !0
         20        FETCH_CONSTANT                                   ~8      'e%5Cbool'
         21        SEND_VAL                                                 ~8
         22        DO_FCALL                                      0  $9      
         23        ASSIGN                                                   !0, $9
   48    24        INIT_FCALL                                               'var_dump'
         25        SEND_VAR                                                 !0
         26        DO_ICALL                                                 
   50    27      > RETURN                                                   1

Function e%5Ccheck:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 19
filename:       /in/6B3hA
function name:  e\check
number of ops:  41
compiled vars:  !0 = $var, !1 = $type, !2 = $check
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        GET_TYPE                                         ~3      !0
          3        IS_IDENTICAL                                             !1, ~3
          4      > JMPZ                                                     ~4, ->6
   16     5    > > RETURN                                                   !0
   19     6    >   INIT_FCALL                                               'sprintf'
          7        SEND_VAL                                                 'is_'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !2, $5
   20    11        INIT_FCALL                                               'function_exists'
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $7      
         14      > JMPZ_EX                                          ~8      $7, ->19
         15    >   INIT_DYNAMIC_CALL                                        !2
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0  $9      
         18        BOOL                                             ~8      $9
         19    > > JMPZ                                                     ~8, ->22
   21    20    > > RETURN                                                   !0
         21*       JMP                                                      ->28
   22    22    >   INIT_FCALL                                               'is_a'
         23        SEND_VAR                                                 !0
         24        SEND_VAR                                                 !1
         25        DO_ICALL                                         $10     
         26      > JMPZ                                                     $10, ->28
   23    27    > > RETURN                                                   !0
   26    28    >   NEW                                              $11     'Exception'
         29        INIT_FCALL                                               'sprintf'
         30        SEND_VAL                                                 'Expected+%25s%2C+got+%25s'
         31        SEND_VAR                                                 !1
         32        INIT_NS_FCALL_BY_NAME                                    'e%5Cgettype'
         33        SEND_VAR_EX                                              !0
         34        DO_FCALL                                      0  $12     
         35        SEND_VAR                                                 $12
         36        DO_ICALL                                         $13     
         37        SEND_VAR_NO_REF_EX                                       $13
         38        DO_FCALL                                      0          
         39      > THROW                                         0          $11
   27    40*     > RETURN                                                   null

End of function e%5Ccheck

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.3 ms | 1403 KiB | 25 Q