3v4l.org

run code in 300+ PHP versions simultaneously
<?php const SOMECONST = 'Const hello'; enum SomeEnum { case Hearts; case Diamonds; case Clubs; case Spades; } function SomeFunction() { } $SomeFunctionClosure = function() { }; $SomeFunctionFirstClassCallable = SomeFunction(...); $someFunctionClosureFromCallabe = Closure::fromCallable('SomeFunction'); class ShouldTypeError { public function __construct($returnValue) { if ($returnValue !== 'no-return') { if ($returnValue === '$this') { return $this; } elseif ($returnValue === 'no-return-value') { return; } elseif ($returnValue === 'void') { return void; } elseif ($returnValue === 'never') { return never; } return $returnValue; } } } $testcases = [ 'no-return', 'no-return-value', '$this', new ShouldTypeError('no-return'), SOMECONST, 'abc', 1, 1.23, null, true, false, ['a', 'b', 'c'], ['a' => 'a', 'b' => 'b', 'c' => 'c', 0 => 'Zero'], 'never', 'void', SomeEnum::Spades, function() { echo 'Hi'.PHP_EOL; }, $SomeFunctionClosure, $SomeFunctionFirstClassCallable, $someFunctionClosureFromCallabe, new DateTimeImmutable(), ]; foreach($testcases as $testcase) { echo "--------------[ TESTCASE ]--------------\n"; var_dump($testcase); echo "\n"; try { $didReturn = new ShouldTypeError($testcase); switch($testcase) { case 'no-return': echo "Success: without a return statement is always valid.\n"; break; case 'no-return-value': echo "Success: a return statement without a value is always valid.\n"; break; case '$this': echo "Dubious: return $this is dubious.\n"; echo "- it fullfills the return type, so it could be allowed.\n"; echo "- but returning anything from a constructor is nonsense, because it is discarded.\n"; echo " As shown by the third testcase new SomeTypeError('no-return').\n"; break; default: echo "Error: why is it not a return TypeError?\n"; break; } if (!($didReturn instanceof ShouldTypeError)) { echo "Failed to new a ShouldTypeError.\n"; } } catch (Throwable $ex) { echo "Success: throwable: ".$ex->getMessage()."\n"; } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 42, Position 2 = 87
Branch analysis from position: 42
2 jumps found. (Code = 78) Position 1 = 43, Position 2 = 87
Branch analysis from position: 43
5 jumps found. (Code = 188) Position 1 = 60, Position 2 = 62, Position 3 = 64, Position 4 = 73, Position 5 = 53
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 78, Position 2 = 79
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 79
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 60
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 62
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 64
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 64
Branch analysis from position: 62
Branch analysis from position: 60
Branch analysis from position: 87
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 87
Found catch point at position: 80
Branch analysis from position: 80
2 jumps found. (Code = 107) Position 1 = 81, Position 2 = -2
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
filename:       /in/sXXe8
function name:  (null)
number of ops:  89
compiled vars:  !0 = $SomeFunctionClosure, !1 = $SomeFunctionFirstClassCallable, !2 = $someFunctionClosureFromCallabe, !3 = $testcases, !4 = $testcase, !5 = $didReturn, !6 = $ex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CONST                                            'SOMECONST', 'Const+hello'
    5     1        DECLARE_CLASS                                            'someenum'
   17     2        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
          3        ASSIGN                                                   !0, ~7
   21     4        INIT_FCALL                                               'somefunction'
          5        ZEND_CALLABLE_CONVERT                            ~9      
          6        ASSIGN                                                   !1, ~9
   23     7        INIT_STATIC_METHOD_CALL                                  'Closure', 'fromCallable'
          8        SEND_VAL                                                 'SomeFunction'
          9        DO_FCALL                                      0  $11     
         10        ASSIGN                                                   !2, $11
   46    11        INIT_ARRAY                                       ~13     'no-return'
   47    12        ADD_ARRAY_ELEMENT                                ~13     'no-return-value'
   48    13        ADD_ARRAY_ELEMENT                                ~13     '%24this'
   49    14        NEW                                              $14     'ShouldTypeError'
         15        SEND_VAL_EX                                              'no-return'
         16        DO_FCALL                                      0          
         17        ADD_ARRAY_ELEMENT                                ~13     $14
   51    18        FETCH_CONSTANT                                   ~16     'SOMECONST'
         19        ADD_ARRAY_ELEMENT                                ~13     ~16
   52    20        ADD_ARRAY_ELEMENT                                ~13     'abc'
   53    21        ADD_ARRAY_ELEMENT                                ~13     1
   54    22        ADD_ARRAY_ELEMENT                                ~13     1.23
   46    23        ADD_ARRAY_ELEMENT                                ~13     null
         24        ADD_ARRAY_ELEMENT                                ~13     <true>
         25        ADD_ARRAY_ELEMENT                                ~13     <false>
         26        ADD_ARRAY_ELEMENT                                ~13     <array>
         27        ADD_ARRAY_ELEMENT                                ~13     <array>
   62    28        ADD_ARRAY_ELEMENT                                ~13     'never'
   63    29        ADD_ARRAY_ELEMENT                                ~13     'void'
   65    30        FETCH_CLASS_CONSTANT                             ~17     'SomeEnum', 'Spades'
         31        ADD_ARRAY_ELEMENT                                ~13     ~17
   67    32        DECLARE_LAMBDA_FUNCTION                          ~18     [1]
   69    33        ADD_ARRAY_ELEMENT                                ~13     ~18
   70    34        ADD_ARRAY_ELEMENT                                ~13     !0
   71    35        ADD_ARRAY_ELEMENT                                ~13     !1
   72    36        ADD_ARRAY_ELEMENT                                ~13     !2
   74    37        NEW                                              $19     'DateTimeImmutable'
         38        DO_FCALL                                      0          
         39        ADD_ARRAY_ELEMENT                                ~13     $19
   45    40        ASSIGN                                                   !3, ~13
   77    41      > FE_RESET_R                                       $22     !3, ->87
         42    > > FE_FETCH_R                                               $22, !4, ->87
   78    43    >   ECHO                                                     '--------------%5B+TESTCASE+%5D--------------%0A'
   79    44        INIT_FCALL                                               'var_dump'
         45        SEND_VAR                                                 !4
         46        DO_ICALL                                                 
   80    47        ECHO                                                     '%0A'
   83    48        NEW                                              $24     'ShouldTypeError'
         49        SEND_VAR_EX                                              !4
         50        DO_FCALL                                      0          
         51        ASSIGN                                                   !5, $24
   85    52      > SWITCH_STRING                                            !4, [ 'no-return':->60, 'no-return-value':->62, '%24this':->64, ], ->73
   86    53    >   IS_EQUAL                                                 !4, 'no-return'
         54      > JMPNZ                                                    ~27, ->60
   90    55    >   IS_EQUAL                                                 !4, 'no-return-value'
         56      > JMPNZ                                                    ~27, ->62
   94    57    >   IS_EQUAL                                                 !4, '%24this'
         58      > JMPNZ                                                    ~27, ->64
         59    > > JMP                                                      ->73
   87    60    >   ECHO                                                     'Success%3A+without+a+return+statement+is+always+valid.%0A'
   88    61      > JMP                                                      ->75
   91    62    >   ECHO                                                     'Success%3A+a+return+statement+without+a+value+is+always+valid.%0A'
   92    63      > JMP                                                      ->75
   95    64    >   ROPE_INIT                                     3  ~30     'Dubious%3A+return+'
         65        FETCH_THIS                                       ~28     
         66        ROPE_ADD                                      1  ~30     ~30, ~28
         67        ROPE_END                                      2  ~29     ~30, '+is+dubious.%0A'
         68        ECHO                                                     ~29
   96    69        ECHO                                                     '-+it+fullfills+the+return+type%2C+so+it+could+be+allowed.%0A'
   97    70        ECHO                                                     '-+but+returning+anything+from+a+constructor+is+nonsense%2C+because+it+is+discarded.%0A'
   98    71        ECHO                                                     '++As+shown+by+the+third+testcase+new+SomeTypeError%28%27no-return%27%29.%0A'
   99    72      > JMP                                                      ->75
  102    73    >   ECHO                                                     'Error%3A+why+is+it+not+a+return+TypeError%3F%0A'
  103    74      > JMP                                                      ->75
  106    75    >   INSTANCEOF                                       ~32     !5, 'ShouldTypeError'
         76        BOOL_NOT                                         ~33     ~32
         77      > JMPZ                                                     ~33, ->79
  107    78    >   ECHO                                                     'Failed+to+new+a+ShouldTypeError.%0A'
         79    > > JMP                                                      ->86
  109    80  E > > CATCH                                       last         'Throwable'
  110    81    >   INIT_METHOD_CALL                                         !6, 'getMessage'
         82        DO_FCALL                                      0  $34     
         83        CONCAT                                           ~35     'Success%3A+throwable%3A+', $34
         84        CONCAT                                           ~36     ~35, '%0A'
         85        ECHO                                                     ~36
   77    86    > > JMP                                                      ->42
         87    >   FE_FREE                                                  $22
  112    88      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sXXe8
function name:  {closure}
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E > > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sXXe8
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   ECHO                                                     'Hi%0A'
   69     1      > RETURN                                                   null

End of Dynamic Function 1

Function somefunction:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sXXe8
function name:  SomeFunction
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E > > RETURN                                                   null

End of function somefunction

Class SomeEnum: [no user functions]
Class ShouldTypeError:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 22
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sXXe8
function name:  __construct
number of ops:  23
compiled vars:  !0 = $returnValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        IS_NOT_IDENTICAL                                         !0, 'no-return'
          2      > JMPZ                                                     ~1, ->22
   30     3    >   IS_IDENTICAL                                             !0, '%24this'
          4      > JMPZ                                                     ~2, ->8
   31     5    >   FETCH_THIS                                       ~3      
          6      > RETURN                                                   ~3
   30     7*       JMP                                                      ->21
   32     8    >   IS_IDENTICAL                                             !0, 'no-return-value'
          9      > JMPZ                                                     ~4, ->12
   33    10    > > RETURN                                                   null
   32    11*       JMP                                                      ->21
   34    12    >   IS_IDENTICAL                                             !0, 'void'
         13      > JMPZ                                                     ~5, ->17
   35    14    >   FETCH_CONSTANT                                   ~6      'void'
         15      > RETURN                                                   ~6
   34    16*       JMP                                                      ->21
   36    17    >   IS_IDENTICAL                                             !0, 'never'
         18      > JMPZ                                                     ~7, ->21
   37    19    >   FETCH_CONSTANT                                   ~8      'never'
         20      > RETURN                                                   ~8
   40    21    > > RETURN                                                   !0
   42    22    > > RETURN                                                   null

End of function __construct

End of class ShouldTypeError.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.31 ms | 1029 KiB | 15 Q