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 string($string, $precision = 4) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); return $number; } /** * Perform an addition operation * @param DecimalNumber $number the number to add * @return DecimalNumber Return the sum */ public function sum(DecimalNumber $number) { return DecimalNumber::string(bcadd($this->number, $number, $this->precision)); } public function floored() { 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::createWithString('4.7'); echo $d->sum(DecimalNumber::createWithString('2.1')); echo $d->floored();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kmXes
function name:  (null)
number of ops:  16
compiled vars:  !0 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   DECLARE_CLASS                                            'decimalnumber'
   52     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'createWithString'
          2        SEND_VAL_EX                                              '4.7'
          3        DO_FCALL                                      0  $1      
          4        ASSIGN                                                   !0, $1
   53     5        INIT_METHOD_CALL                                         !0, 'sum'
          6        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'createWithString'
          7        SEND_VAL_EX                                              '2.1'
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0  $4      
         11        ECHO                                                     $4
   54    12        INIT_METHOD_CALL                                         !0, 'floored'
         13        DO_FCALL                                      0  $5      
         14        ECHO                                                     $5
         15      > RETURN                                                   1

Class DecimalNumber:
Function string:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kmXes
function name:  string
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 string

Function sum:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kmXes
function name:  sum
number of ops:  15
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     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                                                 $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   36    14*     > RETURN                                                   null

End of function sum

Function floored:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kmXes
function name:  floored
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     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
   40    14*     > RETURN                                                   null

End of function floored

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kmXes
function name:  __toString
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   FETCH_OBJ_R                                      ~0      'number'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   48     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:
162.72 ms | 1399 KiB | 16 Q