3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface I { public function a(int $a): string; } Class C implements I { public function a(int|string $a): string { return "foo"; } } class D implements I { public function a(int $a): string { return "foo"; } } /** * `test` from previous example (https://3v4l.org/5YPdg) does not respect I. * It calls with a string instead of an int (which is what I requires), thus the $subject->a() * call will fail at runtime for any implementation of I that doesn't widen the type. */ function ignores_i(I $subject) { print $subject->a("beep"); } /** * This implementation respects the interface--the $subject->a() call will * _never_ result in a type error at runtime */ function respects_i(I $subject) { print $subject->a(123); } /** * Widening int => int|string _is_ useful, becacuse we can also write code against * this concrete type, and here we _can_ safely pass a string. */ function uses_concrete_type(C $subject) { $subject->a("beep"); } respects_i(new C()); // works respects_i(new D()); // works uses_concrete_type(new C()); // works ignores_i(new C()); // works ignores_i(new D()); // type error
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  (null)
number of ops:  28
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   DECLARE_CLASS                                            'c'
   13     1        DECLARE_CLASS                                            'd'
   45     2        INIT_FCALL                                               'respects_i'
          3        NEW                                              $0      'C'
          4        DO_FCALL                                      0          
          5        SEND_VAR                                                 $0
          6        DO_FCALL                                      0          
   46     7        INIT_FCALL                                               'respects_i'
          8        NEW                                              $3      'D'
          9        DO_FCALL                                      0          
         10        SEND_VAR                                                 $3
         11        DO_FCALL                                      0          
   47    12        INIT_FCALL                                               'uses_concrete_type'
         13        NEW                                              $6      'C'
         14        DO_FCALL                                      0          
         15        SEND_VAR                                                 $6
         16        DO_FCALL                                      0          
   48    17        INIT_FCALL                                               'ignores_i'
         18        NEW                                              $9      'C'
         19        DO_FCALL                                      0          
         20        SEND_VAR                                                 $9
         21        DO_FCALL                                      0          
   49    22        INIT_FCALL                                               'ignores_i'
         23        NEW                                              $12     'D'
         24        DO_FCALL                                      0          
         25        SEND_VAR                                                 $12
         26        DO_FCALL                                      0          
         27      > RETURN                                                   1

Function ignores_i:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  ignores_i
number of ops:  6
compiled vars:  !0 = $subject
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        INIT_METHOD_CALL                                         !0, 'a'
          2        SEND_VAL_EX                                              'beep'
          3        DO_FCALL                                      0  $1      
          4        ECHO                                                     $1
   26     5      > RETURN                                                   null

End of function ignores_i

Function respects_i:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  respects_i
number of ops:  6
compiled vars:  !0 = $subject
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        INIT_METHOD_CALL                                         !0, 'a'
          2        SEND_VAL_EX                                              123
          3        DO_FCALL                                      0  $1      
          4        ECHO                                                     $1
   34     5      > RETURN                                                   null

End of function respects_i

Function uses_concrete_type:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  uses_concrete_type
number of ops:  5
compiled vars:  !0 = $subject
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   42     1        INIT_METHOD_CALL                                         !0, 'a'
          2        SEND_VAL_EX                                              'beep'
          3        DO_FCALL                                      0          
   43     4      > RETURN                                                   null

End of function uses_concrete_type

Class I:
Function a:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  a
number of ops:  3
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        VERIFY_RETURN_TYPE                                       
          2      > RETURN                                                   null

End of function a

End of class I.

Class C:
Function a:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  a
number of ops:  4
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
    9     1      > RETURN                                                   'foo'
   10     2*       VERIFY_RETURN_TYPE                                       
          3*     > RETURN                                                   null

End of function a

End of class C.

Class D:
Function a:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qMZOH
function name:  a
number of ops:  4
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1      > RETURN                                                   'foo'
   16     2*       VERIFY_RETURN_TYPE                                       
          3*     > RETURN                                                   null

End of function a

End of class D.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.71 ms | 1003 KiB | 18 Q