3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Immutable class for handling numbers with precision. * * Never change the $number or the $precision after the creation of the object. */ class DecimalNumber { private $precision; private $number; /** * Get an instance given a string and a precision * @param string $string The decimal number in string form * @param int $precision The precision of the number * @return DecimalNumber Return new instance */ public static function get($string, $precision = 4) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); return $number; } /** * Add a formal entry point for decimal numbers * @param DecimalNumber $number Decimal number to base off of * @param integer $precision new precision * @return DecimalNumber DecimalNumber with new precision */ public static function decimalNumber(DecimalNumber $number, $precision = 4) { return static::get($number, $precision); } /** * Perform an addition operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function sum(DecimalNumber $number) { return DecimalNumber::string(bcadd($this->number, $number, $this->precision)); } /** * Perform a subtraction operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function difference(DecimalNumber $number) { return DecimalNumber::string(bcsub($this->number, $number, $this->precision)); } /** * Perform a multiplication operation * @param DecimalNumber $number Multiplier * @return DecimalNumber Product */ public function product(DecimalNumber $number) { return DecimalNumber::string(bcmul($this->number, $number, $this->precision)); } /** * Perform a division operation * @param DecimalNumber $number Divisor * @return DecimalNumber Quotient */ public function quotient(DecimalNumber $number) { return DecimalNumber::string(bcdiv($this->number, $number, $this->precision)); } public function floor() { return bcadd(bcadd($this->number, '0'), '0', $this->precision); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $d = DecimalNumber::string('4.7'); $c = $d->sum(DecimalNumber::string('2.1')); echo $c->floored();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  (null)
number of ops:  16
compiled vars:  !0 = $d, !1 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   DECLARE_CLASS                                            'decimalnumber'
   88     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'string'
          2        SEND_VAL_EX                                              '4.7'
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !0, $2
   89     5        INIT_METHOD_CALL                                         !0, 'sum'
          6        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'string'
          7        SEND_VAL_EX                                              '2.1'
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR_NO_REF_EX                                       $4
         10        DO_FCALL                                      0  $5      
         11        ASSIGN                                                   !1, $5
   90    12        INIT_METHOD_CALL                                         !1, 'floored'
         13        DO_FCALL                                      0  $7      
         14        ECHO                                                     $7
         15      > RETURN                                                   1

Class DecimalNumber:
Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  get
number of ops:  19
compiled vars:  !0 = $string, !1 = $precision, !2 = $me, !3 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      4
   20     2        GET_CALLED_CLASS                                 ~4      
          3        ASSIGN                                                   !2, ~4
   21     4        FETCH_CLASS                                   0  $6      !2
          5        NEW                                              $7      $6
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !3, $7
   23     8        ASSIGN_OBJ                                               !3, 'precision'
          9        OP_DATA                                                  !1
   24    10        INIT_FCALL_BY_NAME                                       'bcadd'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAL_EX                                              '0'
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $12     
         15        ASSIGN_OBJ                                               !3, 'number'
         16        OP_DATA                                                  $12
   26    17      > RETURN                                                   !3
   27    18*     > RETURN                                                   null

End of function get

Function decimalnumber:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  decimalNumber
number of ops:  8
compiled vars:  !0 = $number, !1 = $precision
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      4
   36     2        INIT_STATIC_METHOD_CALL                                  'get'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $2      
          6      > RETURN                                                   $2
   37     7*     > RETURN                                                   null

End of function decimalnumber

Function sum:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  sum
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   45     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'string'
          2        INIT_FCALL_BY_NAME                                       'bcadd'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR_NO_REF_EX                                       $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   46    14*     > RETURN                                                   null

End of function sum

Function difference:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  difference
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'string'
          2        INIT_FCALL_BY_NAME                                       'bcsub'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR_NO_REF_EX                                       $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   54    14*     > RETURN                                                   null

End of function difference

Function product:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  product
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'string'
          2        INIT_FCALL_BY_NAME                                       'bcmul'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR_NO_REF_EX                                       $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   63    14*     > RETURN                                                   null

End of function product

Function quotient:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  quotient
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   71     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'string'
          2        INIT_FCALL_BY_NAME                                       'bcdiv'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $1      'number'
          5        SEND_FUNC_ARG                                            $1
          6        SEND_VAR_EX                                              !0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'precision'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR_NO_REF_EX                                       $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   72    14*     > RETURN                                                   null

End of function quotient

Function floor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9vGKt
function name:  floor
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   INIT_FCALL_BY_NAME                                       'bcadd'
          1        INIT_FCALL_BY_NAME                                       'bcadd'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $0      'number'
          4        SEND_FUNC_ARG                                            $0
          5        SEND_VAL_EX                                              '0'
          6        DO_FCALL                                      0  $1      
          7        SEND_VAR_NO_REF_EX                                       $1
          8        SEND_VAL_EX                                              '0'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_OBJ_FUNC_ARG                               $2      'precision'
         11        SEND_FUNC_ARG                                            $2
         12        DO_FCALL                                      0  $3      
         13      > RETURN                                                   $3
   76    14*     > RETURN                                                   null

End of function floor

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

End of function __tostring

End of class DecimalNumber.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.38 ms | 1407 KiB | 13 Q