3v4l.org

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

End of Dynamic Function 1

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

End of Dynamic Function 2

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

End of function somefunction

Class SpecialReturnValue: [no user functions]
Class ShouldTypeError:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 31
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/svfeO
function name:  __construct
number of ops:  32
compiled vars:  !0 = $returnValue, !1 = $uniqueId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     1        BIND_STATIC                                              !1
   18     2        ASSIGN_OBJ                                               'uniqueId'
          3        OP_DATA                                                  !1
   19     4        PRE_INC                                                  !1
   21     5        FETCH_CLASS_CONSTANT                             ~4      'SpecialReturnValue', 'NoReturn'
          6        IS_NOT_IDENTICAL                                         !0, ~4
          7      > JMPZ                                                     ~5, ->31
   22     8    >   FETCH_CLASS_CONSTANT                             ~6      'SpecialReturnValue', 'ReturnWithoutValue'
          9        IS_IDENTICAL                                             !0, ~6
         10      > JMPZ                                                     ~7, ->13
   23    11    > > RETURN                                                   null
   22    12*       JMP                                                      ->30
   24    13    >   FETCH_CLASS_CONSTANT                             ~8      'SpecialReturnValue', 'ReturnDollarThis'
         14        IS_IDENTICAL                                             !0, ~8
         15      > JMPZ                                                     ~9, ->19
   25    16    >   FETCH_THIS                                       ~10     
         17      > RETURN                                                   ~10
   24    18*       JMP                                                      ->30
   26    19    >   FETCH_CLASS_CONSTANT                             ~11     'SpecialReturnValue', 'ReturnVoid'
         20        IS_IDENTICAL                                             !0, ~11
         21      > JMPZ                                                     ~12, ->25
   27    22    >   FETCH_CONSTANT                                   ~13     'void'
         23      > RETURN                                                   ~13
   26    24*       JMP                                                      ->30
   28    25    >   FETCH_CLASS_CONSTANT                             ~14     'SpecialReturnValue', 'ReturnNever'
         26        IS_IDENTICAL                                             !0, ~14
         27      > JMPZ                                                     ~15, ->30
   29    28    >   FETCH_CONSTANT                                   ~16     'never'
         29      > RETURN                                                   ~16
   32    30    > > RETURN                                                   !0
   34    31    > > RETURN                                                   null

End of function __construct

End of class ShouldTypeError.

Class SomeEnum: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.44 ms | 1034 KiB | 16 Q