3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 createWithString($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 add(DecimalNumber $number) { return DecimalNumber::createWithString(bcadd($this->number, $number, $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('.7'); echo $d->add(DecimalNumber::createWithString('.1'));
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/oSJ1L
function name:  (null)
number of ops:  13
compiled vars:  !0 = $d
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   8     0  E >   NOP                                                      
  48     1        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'createWithString'
         2        SEND_VAL_EX                                              '.7'
         3        DO_FCALL                                      0  $2      
         4        ASSIGN                                                   !0, $2
  49     5        INIT_METHOD_CALL                                         !0, 'add'
         6        INIT_STATIC_METHOD_CALL                                  'DecimalNumber', 'createWithString'
         7        SEND_VAL_EX                                              '.1'
         8        DO_FCALL                                      0  $4      
         9        SEND_VAR_NO_REF                               4          $4
        10        DO_FCALL                                      0  $5      
        11        ECHO                                                     $5
        12      > RETURN                                                   1

Class DecimalNumber:
Function createwithstring:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/oSJ1L
function name:  createWithString
number of ops:  20
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        INIT_FCALL                                               'get_called_class'
         3        DO_ICALL                                         $4      
         4        ASSIGN                                                   !2, $4
  21     5        FETCH_CLASS                                 512  :6      !2
         6        NEW                                              $7      :6
         7        DO_FCALL                                      0          
         8        ASSIGN                                                   !3, $7
  23     9        ASSIGN_OBJ                                               !3, 'precision'
        10        OP_DATA                                                  !1
  24    11        INIT_FCALL                                               'bcadd'
        12        SEND_VAR                                                 !0
        13        SEND_VAL                                                 '0'
        14        SEND_VAR                                                 !1
        15        DO_ICALL                                         $12     
        16        ASSIGN_OBJ                                               !3, 'number'
        17        OP_DATA                                                  $12
  26    18      > RETURN                                                   !3
  27    19*     > RETURN                                                   null

End of function createwithstring

Function add:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/oSJ1L
function name:  add
number of ops:  13
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', 'createWithString'
         2        INIT_FCALL                                               'bcadd'
         3        FETCH_OBJ_R                                      $1      'number'
         4        SEND_VAR                                                 $1
         5        SEND_VAR                                                 !0
         6        FETCH_OBJ_R                                      $2      'precision'
         7        SEND_VAR                                                 $2
         8        DO_ICALL                                         $3      
         9        SEND_VAR_NO_REF                               4          $3
        10        DO_FCALL                                      0  $4      
        11      > RETURN                                                   $4
  36    12*     > RETURN                                                   null

End of function add

Function __tostring:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/oSJ1L
function name:  __toString
number of ops:  3
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  43     0  E >   FETCH_OBJ_R                                      $0      'number'
         1      > RETURN                                                   $0
  44     2*     > RETURN                                                   null

End of function __tostring

End of class DecimalNumber.

Generated using Vulcan Logic Dumper, using php 7.2.0