3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Fraction { const FRACTION_RATIO_ADJUST = true; const FRACTION_RATIO_KEEP = false; const FRACTION_FORMAT_DELIMITER = '/'; private $value = []; public function __construct($x, $y, $adjustRatio=self::FRACTION_RATIO_KEEP) { if(!is_int($x) || !is_int($y) || !$y) { throw InvalidArgumentException('Invalid fraction parts'); } $sign = ($x>0&&$y>0) || ($x<0&&$y<0)?1:-1; $this->value = ['numerator'=>$sign*$x, 'denominator'=>abs($y)]; if($adjustRatio==self::FRACTION_RATIO_ADJUST) { $this->adjustRatio(); } } public function getNumerator() { return $this->value['numerator']; } public function getDenominator() { return $this->value['denominator']; } public function adjustRatio() { $gcd = (int)gmp_strval(gmp_gcd( (string)$this->getNumerator(), (string)$this->getDenominator())); $this->setFraction([ 'numerator' => $this->getNumerator()/$gcd, 'denominator' => $this->getDenominator()/$gcd ]); } public function add(self $fraction, $adjustRatio=self::FRACTION_RATIO_KEEP) { $numerator = $this->getNumerator()*$fraction->getDenominator() + $this->getDenominator()*$fraction->getNumerator(); $denominator = $this->getDenominator()*$fraction->getDenominator(); $this->setFraction([ 'numerator' => $numerator, 'denominator' => $denominator ]); if($adjustRatio==self::FRACTION_RATIO_ADJUST) { $this->adjustRatio(); } } public function format() { $denominator = $this->getDenominator(); $numerator = $this->getNumerator(); return $denominator==1? (string)$numerator: (string)($numerator.self::FRACTION_FORMAT_DELIMITER.$denominator); } public function __toString() { return $this->format();//may be other } private function setFraction(array $data) { if(!array_key_exists('numerator', $data) || !array_key_exists('denominator', $data)) { throw InvalidArgumentException('Invalid data structure used in direct set'); } if(!is_int($data['numerator']) || !is_int($data['denominator']) || !$data['denominator']) { throw InvalidArgumentException('Invalid fraction data used in direct set'); } $this->value['numerator'] = $data['numerator']; $this->value['denominator'] = $data['denominator']; } } $fraction = new Fraction(8, -12, Fraction::FRACTION_RATIO_KEEP); echo($fraction->format());//-8/12 $fraction->add(new Fraction(1, 3), Fraction::FRACTION_RATIO_ADJUST); echo($fraction->format());//-1/3
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PaNiq
function name:  (null)
number of ops:  24
compiled vars:  !0 = $fraction
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'fraction'
   91     1        NEW                                              $1      'Fraction'
          2        SEND_VAL_EX                                              8
          3        SEND_VAL_EX                                              -12
          4        FETCH_CLASS_CONSTANT                             ~2      'Fraction', 'FRACTION_RATIO_KEEP'
          5        SEND_VAL_EX                                              ~2
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $1
   92     8        INIT_METHOD_CALL                                         !0, 'format'
          9        DO_FCALL                                      0  $5      
         10        ECHO                                                     $5
   93    11        INIT_METHOD_CALL                                         !0, 'add'
         12        NEW                                              $6      'Fraction'
         13        SEND_VAL_EX                                              1
         14        SEND_VAL_EX                                              3
         15        DO_FCALL                                      0          
         16        SEND_VAR_NO_REF_EX                                       $6
         17        FETCH_CLASS_CONSTANT                             ~8      'Fraction', 'FRACTION_RATIO_ADJUST'
         18        SEND_VAL_EX                                              ~8
         19        DO_FCALL                                      0          
   94    20        INIT_METHOD_CALL                                         !0, 'format'
         21        DO_FCALL                                      0  $10     
         22        ECHO                                                     $10
         23      > RETURN                                                   1

Class Fraction:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 47) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 46) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
2 jumps found. (Code = 47) Position 1 = 22, Position 2 = 27
Branch analysis from position: 22
2 jumps found. (Code = 46) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 44
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 44
Branch analysis from position: 42
Branch analysis from position: 44
Branch analysis from position: 26
Branch analysis from position: 27
Branch analysis from position: 21
Branch analysis from position: 12
Branch analysis from position: 9
filename:       /in/PaNiq
function name:  __construct
number of ops:  45
compiled vars:  !0 = $x, !1 = $y, !2 = $adjustRatio, !3 = $sign
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <const ast>
   14     3        TYPE_CHECK                                   16  ~4      !0
          4        BOOL_NOT                                         ~5      ~4
          5      > JMPNZ_EX                                         ~5      ~5, ->9
          6    >   TYPE_CHECK                                   16  ~6      !1
          7        BOOL_NOT                                         ~7      ~6
          8        BOOL                                             ~5      ~7
          9    > > JMPNZ_EX                                         ~5      ~5, ->12
         10    >   BOOL_NOT                                         ~8      !1
         11        BOOL                                             ~5      ~8
         12    > > JMPZ                                                     ~5, ->17
   16    13    >   INIT_FCALL_BY_NAME                                       'InvalidArgumentException'
         14        SEND_VAL_EX                                              'Invalid+fraction+parts'
         15        DO_FCALL                                      0  $9      
         16      > THROW                                         0          $9
   18    17    >   IS_SMALLER                                       ~10     0, !0
         18      > JMPZ_EX                                          ~10     ~10, ->21
         19    >   IS_SMALLER                                       ~11     0, !1
         20        BOOL                                             ~10     ~11
         21    > > JMPNZ_EX                                         ~10     ~10, ->27
         22    >   IS_SMALLER                                       ~12     !0, 0
         23      > JMPZ_EX                                          ~12     ~12, ->26
         24    >   IS_SMALLER                                       ~13     !1, 0
         25        BOOL                                             ~12     ~13
         26    >   BOOL                                             ~10     ~12
         27    > > JMPZ                                                     ~10, ->30
         28    >   QM_ASSIGN                                        ~14     1
         29      > JMP                                                      ->31
         30    >   QM_ASSIGN                                        ~14     -1
         31    >   ASSIGN                                                   !3, ~14
   19    32        MUL                                              ~17     !3, !0
         33        INIT_ARRAY                                       ~18     ~17, 'numerator'
         34        INIT_FCALL                                               'abs'
         35        SEND_VAR                                                 !1
         36        DO_ICALL                                         $19     
         37        ADD_ARRAY_ELEMENT                                ~18     $19, 'denominator'
         38        ASSIGN_OBJ                                               'value'
         39        OP_DATA                                                  ~18
   20    40        BOOL                                             ~20     !2
         41      > JMPZ                                                     ~20, ->44
   22    42    >   INIT_METHOD_CALL                                         'adjustRatio'
         43        DO_FCALL                                      0          
   24    44    > > RETURN                                                   null

End of function __construct

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

End of function getnumerator

Function getdenominator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PaNiq
function name:  getDenominator
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   FETCH_OBJ_R                                      ~0      'value'
          1        FETCH_DIM_R                                      ~1      ~0, 'denominator'
          2      > RETURN                                                   ~1
   34     3*     > RETURN                                                   null

End of function getdenominator

Function adjustratio:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PaNiq
function name:  adjustRatio
number of ops:  27
compiled vars:  !0 = $gcd
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   INIT_FCALL_BY_NAME                                       'gmp_strval'
          1        INIT_FCALL_BY_NAME                                       'gmp_gcd'
   39     2        INIT_METHOD_CALL                                         'getNumerator'
          3        DO_FCALL                                      0  $1      
          4        CAST                                          6  ~2      $1
          5        SEND_VAL_EX                                              ~2
   40     6        INIT_METHOD_CALL                                         'getDenominator'
          7        DO_FCALL                                      0  $3      
          8        CAST                                          6  ~4      $3
          9        SEND_VAL_EX                                              ~4
         10        DO_FCALL                                      0  $5      
         11        SEND_VAR_NO_REF_EX                                       $5
         12        DO_FCALL                                      0  $6      
         13        CAST                                          4  ~7      $6
   38    14        ASSIGN                                                   !0, ~7
   41    15        INIT_METHOD_CALL                                         'setFraction'
   42    16        INIT_METHOD_CALL                                         'getNumerator'
         17        DO_FCALL                                      0  $9      
         18        DIV                                              ~10     $9, !0
         19        INIT_ARRAY                                       ~11     ~10, 'numerator'
   43    20        INIT_METHOD_CALL                                         'getDenominator'
         21        DO_FCALL                                      0  $12     
         22        DIV                                              ~13     $12, !0
         23        ADD_ARRAY_ELEMENT                                ~11     ~13, 'denominator'
         24        SEND_VAL_EX                                              ~11
         25        DO_FCALL                                      0          
   45    26      > RETURN                                                   null

End of function adjustratio

Function add:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 29
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/PaNiq
function name:  add
number of ops:  30
compiled vars:  !0 = $fraction, !1 = $adjustRatio, !2 = $numerator, !3 = $denominator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <const ast>
   49     2        INIT_METHOD_CALL                                         'getNumerator'
          3        DO_FCALL                                      0  $4      
          4        INIT_METHOD_CALL                                         !0, 'getDenominator'
          5        DO_FCALL                                      0  $5      
          6        MUL                                              ~6      $4, $5
   50     7        INIT_METHOD_CALL                                         'getDenominator'
          8        DO_FCALL                                      0  $7      
          9        INIT_METHOD_CALL                                         !0, 'getNumerator'
         10        DO_FCALL                                      0  $8      
         11        MUL                                              ~9      $7, $8
         12        ADD                                              ~10     ~6, ~9
   49    13        ASSIGN                                                   !2, ~10
   51    14        INIT_METHOD_CALL                                         'getDenominator'
         15        DO_FCALL                                      0  $12     
         16        INIT_METHOD_CALL                                         !0, 'getDenominator'
         17        DO_FCALL                                      0  $13     
         18        MUL                                              ~14     $12, $13
         19        ASSIGN                                                   !3, ~14
   52    20        INIT_METHOD_CALL                                         'setFraction'
   53    21        INIT_ARRAY                                       ~16     !2, 'numerator'
   54    22        ADD_ARRAY_ELEMENT                                ~16     !3, 'denominator'
         23        SEND_VAL_EX                                              ~16
         24        DO_FCALL                                      0          
   56    25        BOOL                                             ~18     !1
         26      > JMPZ                                                     ~18, ->29
   58    27    >   INIT_METHOD_CALL                                         'adjustRatio'
         28        DO_FCALL                                      0          
   60    29    > > RETURN                                                   null

End of function add

Function format:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PaNiq
function name:  format
number of ops:  17
compiled vars:  !0 = $denominator, !1 = $numerator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   INIT_METHOD_CALL                                         'getDenominator'
          1        DO_FCALL                                      0  $2      
          2        ASSIGN                                                   !0, $2
   65     3        INIT_METHOD_CALL                                         'getNumerator'
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !1, $4
   66     6        IS_EQUAL                                                 !0, 1
          7      > JMPZ                                                     ~6, ->11
   67     8    >   CAST                                          6  ~7      !1
          9        QM_ASSIGN                                        ~8      ~7
         10      > JMP                                                      ->15
   68    11    >   CONCAT                                           ~9      !1, '%2F'
         12        CONCAT                                           ~10     ~9, !0
         13        CAST                                          6  ~11     ~10
         14        QM_ASSIGN                                        ~8      ~11
         15    > > RETURN                                                   ~8
   69    16*     > RETURN                                                   null

End of function format

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PaNiq
function name:  __toString
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   INIT_METHOD_CALL                                         'format'
          1        DO_FCALL                                      0  $0      
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   74     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function __tostring

Function setfraction:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 47) Position 1 = 16, Position 2 = 20
Branch analysis from position: 16
2 jumps found. (Code = 47) Position 1 = 21, Position 2 = 24
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 20
Branch analysis from position: 7
filename:       /in/PaNiq
function name:  setFraction
number of ops:  38
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV                                             !0      
   78     1        ARRAY_KEY_EXISTS                                 ~1      'numerator', !0
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPNZ_EX                                         ~2      ~2, ->7
          4    >   ARRAY_KEY_EXISTS                                 ~3      'denominator', !0
          5        BOOL_NOT                                         ~4      ~3
          6        BOOL                                             ~2      ~4
          7    > > JMPZ                                                     ~2, ->12
   80     8    >   INIT_FCALL_BY_NAME                                       'InvalidArgumentException'
          9        SEND_VAL_EX                                              'Invalid+data+structure+used+in+direct+set'
         10        DO_FCALL                                      0  $5      
         11      > THROW                                         0          $5
   82    12    >   FETCH_DIM_R                                      ~6      !0, 'numerator'
         13        TYPE_CHECK                                   16  ~7      ~6
         14        BOOL_NOT                                         ~8      ~7
         15      > JMPNZ_EX                                         ~8      ~8, ->20
         16    >   FETCH_DIM_R                                      ~9      !0, 'denominator'
         17        TYPE_CHECK                                   16  ~10     ~9
         18        BOOL_NOT                                         ~11     ~10
         19        BOOL                                             ~8      ~11
         20    > > JMPNZ_EX                                         ~8      ~8, ->24
         21    >   FETCH_DIM_R                                      ~12     !0, 'denominator'
         22        BOOL_NOT                                         ~13     ~12
         23        BOOL                                             ~8      ~13
         24    > > JMPZ                                                     ~8, ->29
   84    25    >   INIT_FCALL_BY_NAME                                       'InvalidArgumentException'
         26        SEND_VAL_EX                                              'Invalid+fraction+data+used+in+direct+set'
         27        DO_FCALL                                      0  $14     
         28      > THROW                                         0          $14
   86    29    >   FETCH_DIM_R                                      ~17     !0, 'numerator'
         30        FETCH_OBJ_W                                      $15     'value'
         31        ASSIGN_DIM                                               $15, 'numerator'
         32        OP_DATA                                                  ~17
   87    33        FETCH_DIM_R                                      ~20     !0, 'denominator'
         34        FETCH_OBJ_W                                      $18     'value'
         35        ASSIGN_DIM                                               $18, 'denominator'
         36        OP_DATA                                                  ~20
   88    37      > RETURN                                                   null

End of function setfraction

End of class Fraction.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.23 ms | 1416 KiB | 21 Q