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 $uniqueId; public function __construct($returnValue) { static $uniqueId = 0; $this->uniqueId = $uniqueId; $uniqueId++; 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(), fopen('php://memory', 'w+'), ]; 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 fourth testcase new SomeTypeError('no-return').\n"; break; default: echo "Error: why is it not a return TypeError?\n"; break; } 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 = 47, Position 2 = 92
Branch analysis from position: 47
2 jumps found. (Code = 78) Position 1 = 48, Position 2 = 92
Branch analysis from position: 48
5 jumps found. (Code = 188) Position 1 = 65, Position 2 = 67, Position 3 = 69, Position 4 = 74, Position 5 = 58
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 78, Position 2 = 83
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
1 jumps found. (Code = 42) Position 1 = 91
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 83
1 jumps found. (Code = 42) Position 1 = 91
Branch analysis from position: 91
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 65
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 67
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 69
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
Branch analysis from position: 69
Branch analysis from position: 67
Branch analysis from position: 65
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 92
Found catch point at position: 85
Branch analysis from position: 85
2 jumps found. (Code = 107) Position 1 = 86, Position 2 = -2
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
filename:       /in/T8rkB
function name:  (null)
number of ops:  94
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
   51    11        INIT_ARRAY                                       ~13     'no-return'
   52    12        ADD_ARRAY_ELEMENT                                ~13     'no-return-value'
   53    13        ADD_ARRAY_ELEMENT                                ~13     '%24this'
   54    14        NEW                                              $14     'ShouldTypeError'
         15        SEND_VAL_EX                                              'no-return'
         16        DO_FCALL                                      0          
         17        ADD_ARRAY_ELEMENT                                ~13     $14
   56    18        FETCH_CONSTANT                                   ~16     'SOMECONST'
         19        ADD_ARRAY_ELEMENT                                ~13     ~16
   57    20        ADD_ARRAY_ELEMENT                                ~13     'abc'
   58    21        ADD_ARRAY_ELEMENT                                ~13     1
   59    22        ADD_ARRAY_ELEMENT                                ~13     1.23
   51    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>
   67    28        ADD_ARRAY_ELEMENT                                ~13     'never'
   68    29        ADD_ARRAY_ELEMENT                                ~13     'void'
   70    30        FETCH_CLASS_CONSTANT                             ~17     'SomeEnum', 'Spades'
         31        ADD_ARRAY_ELEMENT                                ~13     ~17
   72    32        DECLARE_LAMBDA_FUNCTION                          ~18     [1]
   74    33        ADD_ARRAY_ELEMENT                                ~13     ~18
   75    34        ADD_ARRAY_ELEMENT                                ~13     !0
   76    35        ADD_ARRAY_ELEMENT                                ~13     !1
   77    36        ADD_ARRAY_ELEMENT                                ~13     !2
   79    37        NEW                                              $19     'DateTimeImmutable'
         38        DO_FCALL                                      0          
         39        ADD_ARRAY_ELEMENT                                ~13     $19
   80    40        INIT_FCALL                                               'fopen'
         41        SEND_VAL                                                 'php%3A%2F%2Fmemory'
         42        SEND_VAL                                                 'w%2B'
         43        DO_ICALL                                         $21     
         44        ADD_ARRAY_ELEMENT                                ~13     $21
   50    45        ASSIGN                                                   !3, ~13
   83    46      > FE_RESET_R                                       $23     !3, ->92
         47    > > FE_FETCH_R                                               $23, !4, ->92
   84    48    >   ECHO                                                     '--------------%5B+TESTCASE+%5D--------------%0A'
   85    49        INIT_FCALL                                               'var_dump'
         50        SEND_VAR                                                 !4
         51        DO_ICALL                                                 
   86    52        ECHO                                                     '%0A'
   89    53        NEW                                              $25     'ShouldTypeError'
         54        SEND_VAR_EX                                              !4
         55        DO_FCALL                                      0          
         56        ASSIGN                                                   !5, $25
   91    57      > SWITCH_STRING                                            !4, [ 'no-return':->65, 'no-return-value':->67, '%24this':->69, ], ->74
   92    58    >   IS_EQUAL                                                 !4, 'no-return'
         59      > JMPNZ                                                    ~28, ->65
   96    60    >   IS_EQUAL                                                 !4, 'no-return-value'
         61      > JMPNZ                                                    ~28, ->67
  100    62    >   IS_EQUAL                                                 !4, '%24this'
         63      > JMPNZ                                                    ~28, ->69
         64    > > JMP                                                      ->74
   93    65    >   ECHO                                                     'Success%3A+without+a+return+statement+is+always+valid.%0A'
   94    66      > JMP                                                      ->76
   97    67    >   ECHO                                                     'Success%3A+a+return+statement+without+a+value+is+always+valid.%0A'
   98    68      > JMP                                                      ->76
  101    69    >   ECHO                                                     'Dubious%3A+return+%24this+is+dubious.%0A'
  102    70        ECHO                                                     '-+it+fullfills+the+return+type%2C+so+it+could+be+allowed.%0A'
  103    71        ECHO                                                     '-+but+returning+anything+from+a+constructor+is+nonsense%2C+because+it+is+discarded.%0A'
  104    72        ECHO                                                     '++As+shown+by+the+fourth+testcase+new+SomeTypeError%28%27no-return%27%29.%0A'
  105    73      > JMP                                                      ->76
  108    74    >   ECHO                                                     'Error%3A+why+is+it+not+a+return+TypeError%3F%0A'
  109    75      > JMP                                                      ->76
  112    76    >   INSTANCEOF                                               !5, 'ShouldTypeError'
         77      > JMPZ                                                     ~29, ->83
  113    78    >   FETCH_OBJ_R                                      ~30     !5, 'uniqueId'
         79        CONCAT                                           ~31     'Created+uniqueId+', ~30
         80        CONCAT                                           ~32     ~31, '%0A'
         81        ECHO                                                     ~32
  112    82      > JMP                                                      ->84
  115    83    >   ECHO                                                     'Failed+to+new+a+ShouldTypeError.%0A'
         84    > > JMP                                                      ->91
  117    85  E > > CATCH                                       last         'Throwable'
  118    86    >   INIT_METHOD_CALL                                         !6, 'getMessage'
         87        DO_FCALL                                      0  $33     
         88        CONCAT                                           ~34     'Success%3A+throwable%3A+', $33
         89        CONCAT                                           ~35     ~34, '%0A'
         90        ECHO                                                     ~35
   83    91    > > JMP                                                      ->47
         92    >   FE_FREE                                                  $23
  120    93      > 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/T8rkB
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/T8rkB
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   ECHO                                                     'Hi%0A'
   74     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/T8rkB
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 = 7, Position 2 = 26
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T8rkB
function name:  __construct
number of ops:  27
compiled vars:  !0 = $returnValue, !1 = $uniqueId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   30     1        BIND_STATIC                                              !1
   31     2        ASSIGN_OBJ                                               'uniqueId'
          3        OP_DATA                                                  !1
   32     4        PRE_INC                                                  !1
   34     5        IS_NOT_IDENTICAL                                         !0, 'no-return'
          6      > JMPZ                                                     ~4, ->26
   35     7    >   IS_IDENTICAL                                             !0, '%24this'
          8      > JMPZ                                                     ~5, ->12
   36     9    >   FETCH_THIS                                       ~6      
         10      > RETURN                                                   ~6
   35    11*       JMP                                                      ->25
   37    12    >   IS_IDENTICAL                                             !0, 'no-return-value'
         13      > JMPZ                                                     ~7, ->16
   38    14    > > RETURN                                                   null
   37    15*       JMP                                                      ->25
   39    16    >   IS_IDENTICAL                                             !0, 'void'
         17      > JMPZ                                                     ~8, ->21
   40    18    >   FETCH_CONSTANT                                   ~9      'void'
         19      > RETURN                                                   ~9
   39    20*       JMP                                                      ->25
   41    21    >   IS_IDENTICAL                                             !0, 'never'
         22      > JMPZ                                                     ~10, ->25
   42    23    >   FETCH_CONSTANT                                   ~11     'never'
         24      > RETURN                                                   ~11
   45    25    > > RETURN                                                   !0
   47    26    > > RETURN                                                   null

End of function __construct

End of class ShouldTypeError.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.34 ms | 1030 KiB | 16 Q