3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Number { abstract public function primitive(): mixed; abstract public function add(Number $other): Number; abstract protected function addIntNumber(IntNumber $other): Number; abstract protected function addFloatNumber(FloatNumber $other): Number; public static function from(int|float $primitive): Number { return match (true) { is_int($primitive) => new IntNumber($primitive), is_float($primitive) => new FloatNumber($primitive), default => throw new InvalidArgumentException('blah blah'), }; } } class IntNumber extends Number { public function __construct( private int $value ) {} public function primitive(): int { return $this->value; } public function add(Number $other): Number { return $other->addIntNumber($this); } protected function addIntNumber(IntNumber $other): IntNumber { return new IntNumber($this->primitive() + $other->primitive()); } protected function addFloatNumber(FloatNumber $other): FloatNumber { return new FloatNumber((float) $this->primitive() + $other->primitive()); } } class FloatNumber extends Number { public function __construct( private float $value ) {} public function primitive(): float { return $this->value; } public function add(Number $other): Number { return $other->addFloatNumber($this); } protected function addIntNumber(IntNumber $other): FloatNumber { return new FloatNumber($this->primitive() + (float) $other->primitive()); } protected function addFloatNumber(FloatNumber $other): FloatNumber { return new FloatNumber($this->primitive() + $other->primitive()); } } $primitives = [1, 2, 7.13, 5.012]; $numbers = array_map([Number::class, 'from'], $primitives); $carry = new IntNumber(0); foreach ($numbers as $number) { $carry = $carry->add($number); echo '$number: ', get_class($number), ' ', $number->primitive(), PHP_EOL; echo '$carry: ', get_class($carry), ' ', $carry->primitive(), PHP_EOL; echo PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 35
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 35
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/ntNgK
function name:  (null)
number of ops:  37
compiled vars:  !0 = $primitives, !1 = $numbers, !2 = $carry, !3 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   DECLARE_CLASS                                            'intnumber', 'number'
   74     1        ASSIGN                                                   !0, <array>
   76     2        INIT_FCALL                                               'array_map'
          3        SEND_VAL                                                 <array>
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !1, $5
   78     7        NEW                                              $7      'IntNumber'
          8        SEND_VAL_EX                                              0
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $7
   79    11      > FE_RESET_R                                       $10     !1, ->35
         12    > > FE_FETCH_R                                               $10, !3, ->35
   80    13    >   INIT_METHOD_CALL                                         !2, 'add'
         14        SEND_VAR_EX                                              !3
         15        DO_FCALL                                      0  $11     
         16        ASSIGN                                                   !2, $11
   82    17        ECHO                                                     '%24number%3A+'
         18        GET_CLASS                                        ~13     !3
         19        ECHO                                                     ~13
         20        ECHO                                                     '+'
         21        INIT_METHOD_CALL                                         !3, 'primitive'
         22        DO_FCALL                                      0  $14     
         23        ECHO                                                     $14
         24        ECHO                                                     '%0A'
   83    25        ECHO                                                     '%24carry%3A+'
         26        GET_CLASS                                        ~15     !2
         27        ECHO                                                     ~15
         28        ECHO                                                     '+'
         29        INIT_METHOD_CALL                                         !2, 'primitive'
         30        DO_FCALL                                      0  $16     
         31        ECHO                                                     $16
         32        ECHO                                                     '%0A'
   84    33        ECHO                                                     '%0A'
   79    34      > JMP                                                      ->12
         35    >   FE_FREE                                                  $10
   85    36      > RETURN                                                   1

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

End of function primitive

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

End of function add

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

End of function addintnumber

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

End of function addfloatnumber

Function from:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
filename:       /in/ntNgK
function name:  from
number of ops:  28
compiled vars:  !0 = $primitive
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   15     1        TYPE_CHECK                                   16  ~2      !0
          2        IS_IDENTICAL                                             ~2, <true>
          3      > JMPNZ                                                    ~1, ->8
   16     4    >   TYPE_CHECK                                   32  ~3      !0
          5        IS_IDENTICAL                                             ~3, <true>
          6      > JMPNZ                                                    ~1, ->13
          7    > > JMP                                                      ->18
   15     8    >   NEW                                              $4      'IntNumber'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
         11        QM_ASSIGN                                        ~6      $4
         12      > JMP                                                      ->24
   16    13    >   NEW                                              $7      'FloatNumber'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
         16        QM_ASSIGN                                        ~6      $7
         17      > JMP                                                      ->24
   17    18    >   NEW                                              $9      'InvalidArgumentException'
         19        SEND_VAL_EX                                              'blah+blah'
         20        DO_FCALL                                      0          
         21      > THROW                                         1          $9
         22*       QM_ASSIGN                                        ~6      <true>
         23*       JMP                                                      ->24
         24    >   VERIFY_RETURN_TYPE                                       ~6
         25      > RETURN                                                   ~6
   19    26*       VERIFY_RETURN_TYPE                                       
         27*     > RETURN                                                   null

End of function from

End of class Number.

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

End of function __construct

Function primitive:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  primitive
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   FETCH_OBJ_R                                      ~0      'value'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   30     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function primitive

Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  add
number of ops:  9
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   34     1        INIT_METHOD_CALL                                         !0, 'addIntNumber'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5        VERIFY_RETURN_TYPE                                       $2
          6      > RETURN                                                   $2
   35     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function add

Function addintnumber:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  addIntNumber
number of ops:  13
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   39     1        NEW                                              $1      'IntNumber'
          2        INIT_METHOD_CALL                                         'primitive'
          3        DO_FCALL                                      0  $2      
          4        INIT_METHOD_CALL                                         !0, 'primitive'
          5        DO_FCALL                                      0  $3      
          6        ADD                                              ~4      $2, $3
          7        SEND_VAL_EX                                              ~4
          8        DO_FCALL                                      0          
          9        VERIFY_RETURN_TYPE                                       $1
         10      > RETURN                                                   $1
   40    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function addintnumber

Function addfloatnumber:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  addFloatNumber
number of ops:  14
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   44     1        NEW                                              $1      'FloatNumber'
          2        INIT_METHOD_CALL                                         'primitive'
          3        DO_FCALL                                      0  $2      
          4        CAST                                          5  ~3      $2
          5        INIT_METHOD_CALL                                         !0, 'primitive'
          6        DO_FCALL                                      0  $4      
          7        ADD                                              ~5      ~3, $4
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0          
         10        VERIFY_RETURN_TYPE                                       $1
         11      > RETURN                                                   $1
   45    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function addfloatnumber

End of class IntNumber.

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

End of function __construct

Function primitive:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  primitive
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   FETCH_OBJ_R                                      ~0      'value'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   56     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function primitive

Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  add
number of ops:  9
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   60     1        INIT_METHOD_CALL                                         !0, 'addFloatNumber'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5        VERIFY_RETURN_TYPE                                       $2
          6      > RETURN                                                   $2
   61     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function add

Function addintnumber:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  addIntNumber
number of ops:  14
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   65     1        NEW                                              $1      'FloatNumber'
          2        INIT_METHOD_CALL                                         'primitive'
          3        DO_FCALL                                      0  $2      
          4        INIT_METHOD_CALL                                         !0, 'primitive'
          5        DO_FCALL                                      0  $3      
          6        CAST                                          5  ~4      $3
          7        ADD                                              ~5      $2, ~4
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0          
         10        VERIFY_RETURN_TYPE                                       $1
         11      > RETURN                                                   $1
   66    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function addintnumber

Function addfloatnumber:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ntNgK
function name:  addFloatNumber
number of ops:  13
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
   70     1        NEW                                              $1      'FloatNumber'
          2        INIT_METHOD_CALL                                         'primitive'
          3        DO_FCALL                                      0  $2      
          4        INIT_METHOD_CALL                                         !0, 'primitive'
          5        DO_FCALL                                      0  $3      
          6        ADD                                              ~4      $2, $3
          7        SEND_VAL_EX                                              ~4
          8        DO_FCALL                                      0          
          9        VERIFY_RETURN_TYPE                                       $1
         10      > RETURN                                                   $1
   71    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function addfloatnumber

Function from:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
filename:       /in/ntNgK
function name:  from
number of ops:  28
compiled vars:  !0 = $primitive
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   15     1        TYPE_CHECK                                   16  ~2      !0
          2        IS_IDENTICAL                                             ~2, <true>
          3      > JMPNZ                                                    ~1, ->8
   16     4    >   TYPE_CHECK                                   32  ~3      !0
          5        IS_IDENTICAL                                             ~3, <true>
          6      > JMPNZ                                                    ~1, ->13
          7    > > JMP                                                      ->18
   15     8    >   NEW                                              $4      'IntNumber'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
         11        QM_ASSIGN                                        ~6      $4
         12      > JMP                                                      ->24
   16    13    >   NEW                                              $7      'FloatNumber'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
         16        QM_ASSIGN                                        ~6      $7
         17      > JMP                                                      ->24
   17    18    >   NEW                                              $9      'InvalidArgumentException'
         19        SEND_VAL_EX                                              'blah+blah'
         20        DO_FCALL                                      0          
         21      > THROW                                         1          $9
         22*       QM_ASSIGN                                        ~6      <true>
         23*       JMP                                                      ->24
         24    >   VERIFY_RETURN_TYPE                                       ~6
         25      > RETURN                                                   ~6
   19    26*       VERIFY_RETURN_TYPE                                       
         27*     > RETURN                                                   null

End of function from

End of class FloatNumber.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.01 ms | 1023 KiB | 14 Q