3v4l.org

run code in 300+ PHP versions simultaneously
<?php class BigDecimal { private $value; private $scale; function __construct(string $value){ if(preg_match('@^[0-9]*(?:.[0-9]*)?$@', $value) !== 1){ throw new Error("Invalid BigDecimal value $value"); } $this->value = $value; $this->scale = $this->getScaleOf($value); echo "scale of $value is " . $this->scale . "\n"; } private function getScale(BigDecimal $other){ return max($this->scale, $other->scale); } private function getScaleOf(string $value){ $pos = strrpos($value, '.'); if($pos === false){ return 0; } return strlen($value) - $pos - 1; } function valueEquals($other){ } function add(BigDecimal $other){ return new self(bcadd($this, $other, $this->getScale($other))); } function div(BigDecimal $other){ return new self(bcdiv($this, $other, $this->getScale($other))); } function mod(BigDecimal $other){ // not sure if mod or rem return new self(bcmod($this, $other, $this->getScale($other))); } function mul(BigDecimal $other){ return new self(bcmul($this, $other, $this->getScale($other))); } function pow(BigDecimal $other){ return new self(bcpow($this, $other, $this->getScale($other))); } function sub(BigDecimal $other){ return new self(bcsub($this, $other, $this->getScale($other))); } function __toString(){ return $this->value; } } function d(string $value){ return new BigDecimal($value); } echo d('10.123')->sub(d('5.02'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  (null)
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'bigdecimal'
   65     1        INIT_FCALL                                               'd'
          2        SEND_VAL                                                 '10.123'
          3        DO_FCALL                                      0  $0      
          4        INIT_METHOD_CALL                                         $0, 'sub'
          5        INIT_FCALL                                               'd'
          6        SEND_VAL                                                 '5.02'
          7        DO_FCALL                                      0  $1      
          8        SEND_VAR_NO_REF_EX                                       $1
          9        DO_FCALL                                      0  $2      
         10        ECHO                                                     $2
         11      > RETURN                                                   1

Function d:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  d
number of ops:  6
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        NEW                                              $1      'BigDecimal'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   63     5*     > RETURN                                                   null

End of function d

Class BigDecimal:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  __construct
number of ops:  28
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
    9     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%40%5E%5B0-9%5D%2A%28%3F%3A.%5B0-9%5D%2A%29%3F%24%40'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        IS_NOT_IDENTICAL                                         $1, 1
          6      > JMPZ                                                     ~2, ->13
   10     7    >   NEW                                              $3      'Error'
          8        NOP                                                      
          9        FAST_CONCAT                                      ~4      'Invalid+BigDecimal+value+', !0
         10        SEND_VAL_EX                                              ~4
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $3
   12    13    >   ASSIGN_OBJ                                               'value'
         14        OP_DATA                                                  !0
   13    15        INIT_METHOD_CALL                                         'getScaleOf'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0  $8      
         18        ASSIGN_OBJ                                               'scale'
         19        OP_DATA                                                  $8
   15    20        ROPE_INIT                                     3  ~10     'scale+of+'
         21        ROPE_ADD                                      1  ~10     ~10, !0
         22        ROPE_END                                      2  ~9      ~10, '+is+'
         23        FETCH_OBJ_R                                      ~12     'scale'
         24        CONCAT                                           ~13     ~9, ~12
         25        CONCAT                                           ~14     ~13, '%0A'
         26        ECHO                                                     ~14
   16    27      > RETURN                                                   null

End of function __construct

Function getscale:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  getScale
number of ops:  9
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   19     1        INIT_FCALL                                               'max'
          2        FETCH_OBJ_R                                      ~1      'scale'
          3        SEND_VAL                                                 ~1
          4        FETCH_OBJ_R                                      ~2      !0, 'scale'
          5        SEND_VAL                                                 ~2
          6        DO_ICALL                                         $3      
          7      > RETURN                                                   $3
   20     8*     > RETURN                                                   null

End of function getscale

Function getscaleof:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  getScaleOf
number of ops:  14
compiled vars:  !0 = $value, !1 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   23     1        INIT_FCALL                                               'strrpos'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '.'
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !1, $2
   24     6        TYPE_CHECK                                    4          !1
          7      > JMPZ                                                     ~4, ->9
          8    > > RETURN                                                   0
   25     9    >   STRLEN                                           ~5      !0
         10        SUB                                              ~6      ~5, !1
         11        SUB                                              ~7      ~6, 1
         12      > RETURN                                                   ~7
   26    13*     > RETURN                                                   null

End of function getscaleof

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

End of function valueequals

Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  add
number of ops:  15
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   33     1        NEW                          self                $1      
          2        INIT_FCALL_BY_NAME                                       'bcadd'
          3        FETCH_THIS                                       $2      
          4        SEND_VAR_EX                                              $2
          5        SEND_VAR_EX                                              !0
          6        INIT_METHOD_CALL                                         'getScale'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR_NO_REF_EX                                       $4
         12        DO_FCALL                                      0          
         13      > RETURN                                                   $1
   34    14*     > RETURN                                                   null

End of function add

Function div:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  div
number of ops:  15
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   37     1        NEW                          self                $1      
          2        INIT_FCALL_BY_NAME                                       'bcdiv'
          3        FETCH_THIS                                       $2      
          4        SEND_VAR_EX                                              $2
          5        SEND_VAR_EX                                              !0
          6        INIT_METHOD_CALL                                         'getScale'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR_NO_REF_EX                                       $4
         12        DO_FCALL                                      0          
         13      > RETURN                                                   $1
   38    14*     > RETURN                                                   null

End of function div

Function mod:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  mod
number of ops:  15
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        NEW                          self                $1      
          2        INIT_FCALL_BY_NAME                                       'bcmod'
          3        FETCH_THIS                                       $2      
          4        SEND_VAR_EX                                              $2
          5        SEND_VAR_EX                                              !0
          6        INIT_METHOD_CALL                                         'getScale'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR_NO_REF_EX                                       $4
         12        DO_FCALL                                      0          
         13      > RETURN                                                   $1
   42    14*     > RETURN                                                   null

End of function mod

Function mul:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  mul
number of ops:  15
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   45     1        NEW                          self                $1      
          2        INIT_FCALL_BY_NAME                                       'bcmul'
          3        FETCH_THIS                                       $2      
          4        SEND_VAR_EX                                              $2
          5        SEND_VAR_EX                                              !0
          6        INIT_METHOD_CALL                                         'getScale'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR_NO_REF_EX                                       $4
         12        DO_FCALL                                      0          
         13      > RETURN                                                   $1
   46    14*     > RETURN                                                   null

End of function mul

Function pow:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  pow
number of ops:  15
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1        NEW                          self                $1      
          2        INIT_FCALL_BY_NAME                                       'bcpow'
          3        FETCH_THIS                                       $2      
          4        SEND_VAR_EX                                              $2
          5        SEND_VAR_EX                                              !0
          6        INIT_METHOD_CALL                                         'getScale'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR_NO_REF_EX                                       $4
         12        DO_FCALL                                      0          
         13      > RETURN                                                   $1
   50    14*     > RETURN                                                   null

End of function pow

Function sub:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4MecV
function name:  sub
number of ops:  15
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        NEW                          self                $1      
          2        INIT_FCALL_BY_NAME                                       'bcsub'
          3        FETCH_THIS                                       $2      
          4        SEND_VAR_EX                                              $2
          5        SEND_VAR_EX                                              !0
          6        INIT_METHOD_CALL                                         'getScale'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        SEND_VAR_NO_REF_EX                                       $4
         12        DO_FCALL                                      0          
         13      > RETURN                                                   $1
   54    14*     > RETURN                                                   null

End of function sub

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

End of function __tostring

End of class BigDecimal.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.92 ms | 1415 KiB | 21 Q