3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Distance { public const UNIT_KILOMETERS = 'kilometers'; public const UNIT_MILES = 'miles'; private const KILOMETERS_IN_1MILE = 1.609344; private float $distance = 0.0; private string $unit = self::UNIT_KILOMETERS; public function __construct(float $distance, string $unit = self::UNIT_KILOMETERS) { $this->set_distance($distance, $unit); } public function set_distance($distance, string $unit = ''): object { if (empty($unit)) { $unit = $this->unit; } if (!in_array($unit, [self::UNIT_KILOMETERS, self::UNIT_MILES])) { throw new Exception("Invalid Unit [$unit]"); } $this->distance = $distance; $this->unit = $unit; return $this; } public function get_distance(string $unit = ''): float { if (empty($unit)) { $unit = $this->unit; } if ($unit == $this->unit) { return $this->distance; } switch ($unit) { case self::UNIT_KILOMETERS: return $this->distance * self::KILOMETERS_IN_1MILE; case self::UNIT_MILES: return $this->distance / self::KILOMETERS_IN_1MILE; default: throw new Exception("Invalid Unit [$unit]"); } return 0.0; } public function distance_format(string $unit = '', int $decimal_point = 2, string $decimal_separator = '.', $thousands_separator = ','): string { return number_format($this->get_distance($unit), $decimal_point, $decimal_separator, $thousands_separator) . " " . $unit; } } $distance = new Distance(10.0, Distance::UNIT_KILOMETERS); echo $distance->get_distance() . PHP_EOL; echo $distance->get_distance(Distance::UNIT_KILOMETERS) . PHP_EOL; echo $distance->get_distance(Distance::UNIT_MILES) . PHP_EOL; echo $distance->distance_format() . PHP_EOL; echo $distance->distance_format(Distance::UNIT_KILOMETERS) . PHP_EOL; echo $distance->distance_format(Distance::UNIT_MILES) . PHP_EOL; $distance->set_distance(25); echo $distance->get_distance() . PHP_EOL; echo $distance->get_distance(Distance::UNIT_KILOMETERS) . PHP_EOL; echo $distance->get_distance(Distance::UNIT_MILES) . PHP_EOL; echo $distance->distance_format() . PHP_EOL; echo $distance->distance_format(Distance::UNIT_KILOMETERS) . PHP_EOL; echo $distance->distance_format(Distance::UNIT_MILES) . PHP_EOL; $distance->set_distance(10, Distance::UNIT_MILES); echo $distance->get_distance() . PHP_EOL; echo $distance->get_distance(Distance::UNIT_KILOMETERS) . PHP_EOL; echo $distance->get_distance(Distance::UNIT_MILES) . PHP_EOL; echo $distance->distance_format() . PHP_EOL; echo $distance->distance_format(Distance::UNIT_KILOMETERS) . PHP_EOL; echo $distance->distance_format(Distance::UNIT_MILES) . PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CJSEg
function name:  (null)
number of ops:  97
compiled vars:  !0 = $distance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   NEW                                              $1      'Distance'
          1        SEND_VAL_EX                                              10
          2        SEND_VAL_EX                                              'kilometers'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
   67     5        INIT_METHOD_CALL                                         !0, 'get_distance'
          6        DO_FCALL                                      0  $4      
          7        CONCAT                                           ~5      $4, '%0A'
          8        ECHO                                                     ~5
   68     9        INIT_METHOD_CALL                                         !0, 'get_distance'
         10        SEND_VAL_EX                                              'kilometers'
         11        DO_FCALL                                      0  $6      
         12        CONCAT                                           ~7      $6, '%0A'
         13        ECHO                                                     ~7
   69    14        INIT_METHOD_CALL                                         !0, 'get_distance'
         15        SEND_VAL_EX                                              'miles'
         16        DO_FCALL                                      0  $8      
         17        CONCAT                                           ~9      $8, '%0A'
         18        ECHO                                                     ~9
   70    19        INIT_METHOD_CALL                                         !0, 'distance_format'
         20        DO_FCALL                                      0  $10     
         21        CONCAT                                           ~11     $10, '%0A'
         22        ECHO                                                     ~11
   71    23        INIT_METHOD_CALL                                         !0, 'distance_format'
         24        SEND_VAL_EX                                              'kilometers'
         25        DO_FCALL                                      0  $12     
         26        CONCAT                                           ~13     $12, '%0A'
         27        ECHO                                                     ~13
   72    28        INIT_METHOD_CALL                                         !0, 'distance_format'
         29        SEND_VAL_EX                                              'miles'
         30        DO_FCALL                                      0  $14     
         31        CONCAT                                           ~15     $14, '%0A'
         32        ECHO                                                     ~15
   73    33        INIT_METHOD_CALL                                         !0, 'set_distance'
         34        SEND_VAL_EX                                              25
         35        DO_FCALL                                      0          
   74    36        INIT_METHOD_CALL                                         !0, 'get_distance'
         37        DO_FCALL                                      0  $17     
         38        CONCAT                                           ~18     $17, '%0A'
         39        ECHO                                                     ~18
   75    40        INIT_METHOD_CALL                                         !0, 'get_distance'
         41        SEND_VAL_EX                                              'kilometers'
         42        DO_FCALL                                      0  $19     
         43        CONCAT                                           ~20     $19, '%0A'
         44        ECHO                                                     ~20
   76    45        INIT_METHOD_CALL                                         !0, 'get_distance'
         46        SEND_VAL_EX                                              'miles'
         47        DO_FCALL                                      0  $21     
         48        CONCAT                                           ~22     $21, '%0A'
         49        ECHO                                                     ~22
   77    50        INIT_METHOD_CALL                                         !0, 'distance_format'
         51        DO_FCALL                                      0  $23     
         52        CONCAT                                           ~24     $23, '%0A'
         53        ECHO                                                     ~24
   78    54        INIT_METHOD_CALL                                         !0, 'distance_format'
         55        SEND_VAL_EX                                              'kilometers'
         56        DO_FCALL                                      0  $25     
         57        CONCAT                                           ~26     $25, '%0A'
         58        ECHO                                                     ~26
   79    59        INIT_METHOD_CALL                                         !0, 'distance_format'
         60        SEND_VAL_EX                                              'miles'
         61        DO_FCALL                                      0  $27     
         62        CONCAT                                           ~28     $27, '%0A'
         63        ECHO                                                     ~28
   80    64        INIT_METHOD_CALL                                         !0, 'set_distance'
         65        SEND_VAL_EX                                              10
         66        SEND_VAL_EX                                              'miles'
         67        DO_FCALL                                      0          
   81    68        INIT_METHOD_CALL                                         !0, 'get_distance'
         69        DO_FCALL                                      0  $30     
         70        CONCAT                                           ~31     $30, '%0A'
         71        ECHO                                                     ~31
   82    72        INIT_METHOD_CALL                                         !0, 'get_distance'
         73        SEND_VAL_EX                                              'kilometers'
         74        DO_FCALL                                      0  $32     
         75        CONCAT                                           ~33     $32, '%0A'
         76        ECHO                                                     ~33
   83    77        INIT_METHOD_CALL                                         !0, 'get_distance'
         78        SEND_VAL_EX                                              'miles'
         79        DO_FCALL                                      0  $34     
         80        CONCAT                                           ~35     $34, '%0A'
         81        ECHO                                                     ~35
   84    82        INIT_METHOD_CALL                                         !0, 'distance_format'
         83        DO_FCALL                                      0  $36     
         84        CONCAT                                           ~37     $36, '%0A'
         85        ECHO                                                     ~37
   85    86        INIT_METHOD_CALL                                         !0, 'distance_format'
         87        SEND_VAL_EX                                              'kilometers'
         88        DO_FCALL                                      0  $38     
         89        CONCAT                                           ~39     $38, '%0A'
         90        ECHO                                                     ~39
   86    91        INIT_METHOD_CALL                                         !0, 'distance_format'
         92        SEND_VAL_EX                                              'miles'
         93        DO_FCALL                                      0  $40     
         94        CONCAT                                           ~41     $40, '%0A'
         95        ECHO                                                     ~41
   87    96      > RETURN                                                   1

Class Distance:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CJSEg
function name:  __construct
number of ops:  7
compiled vars:  !0 = $distance, !1 = $unit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <const ast>
   16     2        INIT_METHOD_CALL                                         'set_distance'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   17     6      > RETURN                                                   null

End of function __construct

Function set_distance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/CJSEg
function name:  set_distance
number of ops:  25
compiled vars:  !0 = $distance, !1 = $unit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   21     2        ISSET_ISEMPTY_CV                                         !1
          3      > JMPZ                                                     ~2, ->6
   22     4    >   FETCH_OBJ_R                                      ~3      'unit'
          5        ASSIGN                                                   !1, ~3
   24     6    >   IN_ARRAY                                         ~5      !1, <array>
          7        BOOL_NOT                                         ~6      ~5
          8      > JMPZ                                                     ~6, ->16
   25     9    >   NEW                                              $7      'Exception'
         10        ROPE_INIT                                     3  ~9      'Invalid+Unit+%5B'
         11        ROPE_ADD                                      1  ~9      ~9, !1
         12        ROPE_END                                      2  ~8      ~9, '%5D'
         13        SEND_VAL_EX                                              ~8
         14        DO_FCALL                                      0          
         15      > THROW                                         0          $7
   27    16    >   ASSIGN_OBJ                                               'distance'
         17        OP_DATA                                                  !0
   28    18        ASSIGN_OBJ                                               'unit'
         19        OP_DATA                                                  !1
   29    20        FETCH_THIS                                       ~14     
         21        VERIFY_RETURN_TYPE                                       ~14
         22      > RETURN                                                   ~14
   30    23*       VERIFY_RETURN_TYPE                                       
         24*     > RETURN                                                   null

End of function set_distance

Function get_distance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
4 jumps found. (Code = 188) Position 1 = 17, Position 2 = 21, Position 3 = 25, Position 4 = 12
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 21
Branch analysis from position: 17
Branch analysis from position: 5
filename:       /in/CJSEg
function name:  get_distance
number of ops:  35
compiled vars:  !0 = $unit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV_INIT                                        !0      ''
   34     1        ISSET_ISEMPTY_CV                                         !0
          2      > JMPZ                                                     ~1, ->5
   35     3    >   FETCH_OBJ_R                                      ~2      'unit'
          4        ASSIGN                                                   !0, ~2
   37     5    >   FETCH_OBJ_R                                      ~4      'unit'
          6        IS_EQUAL                                                 !0, ~4
          7      > JMPZ                                                     ~5, ->11
   38     8    >   FETCH_OBJ_R                                      ~6      'distance'
          9        VERIFY_RETURN_TYPE                                       ~6
         10      > RETURN                                                   ~6
   40    11    > > SWITCH_STRING                                            !0, [ 'kilometers':->17, 'miles':->21, ], ->25
         12    >   IS_EQUAL                                                 !0, 'kilometers'
         13      > JMPNZ                                                    ~7, ->17
         14    >   IS_EQUAL                                                 !0, 'miles'
         15      > JMPNZ                                                    ~7, ->21
         16    > > JMP                                                      ->25
   42    17    >   FETCH_OBJ_R                                      ~8      'distance'
         18        MUL                                              ~9      ~8, 1.60934
         19        VERIFY_RETURN_TYPE                                       ~9
         20      > RETURN                                                   ~9
   44    21    >   FETCH_OBJ_R                                      ~10     'distance'
         22        DIV                                              ~11     ~10, 1.60934
         23        VERIFY_RETURN_TYPE                                       ~11
         24      > RETURN                                                   ~11
   46    25    >   NEW                                              $12     'Exception'
         26        ROPE_INIT                                     3  ~14     'Invalid+Unit+%5B'
         27        ROPE_ADD                                      1  ~14     ~14, !0
         28        ROPE_END                                      2  ~13     ~14, '%5D'
         29        SEND_VAL_EX                                              ~13
         30        DO_FCALL                                      0          
         31      > THROW                                         0          $12
   48    32*       RETURN                                                   0
   49    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function get_distance

Function distance_format:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CJSEg
function name:  distance_format
number of ops:  19
compiled vars:  !0 = $unit, !1 = $decimal_point, !2 = $decimal_separator, !3 = $thousands_separator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV_INIT                                        !0      ''
          1        RECV_INIT                                        !1      2
          2        RECV_INIT                                        !2      '.'
          3        RECV_INIT                                        !3      '%2C'
   56     4        INIT_FCALL                                               'number_format'
          5        INIT_METHOD_CALL                                         'get_distance'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $4      
          8        SEND_VAR                                                 $4
   57     9        SEND_VAR                                                 !1
   58    10        SEND_VAR                                                 !2
   59    11        SEND_VAR                                                 !3
   56    12        DO_ICALL                                         $5      
   60    13        CONCAT                                           ~6      $5, '+'
   61    14        CONCAT                                           ~7      ~6, !0
         15        VERIFY_RETURN_TYPE                                       ~7
         16      > RETURN                                                   ~7
   62    17*       VERIFY_RETURN_TYPE                                       
         18*     > RETURN                                                   null

End of function distance_format

End of class Distance.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.92 ms | 1017 KiB | 14 Q