3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Coordinate { /** @var float $latitudeRadians */ protected $latitudeRadians; /** @var float $longitudeRadians */ protected $longitudeRadians; /** * Constructor. * * @param float $latitudeRadians * @param float $latitudeRadians */ public function __construct( float $latitudeRadians, float $longitudeRadians ) { $this->latitudeRadians = $latitudeRadians; $this->longitudeRadians = $longitudeRadians; } /** * Create a Coordinate object from latitude and longitude given in degrees. * * @param float $latitudeDegrees * @param float $longitudeDegrees * * @return static */ public static function fromDegrees(float $latitudeDegrees, float $longitudeDegrees) { return new static( $latitudeDegrees * M_PI / 180, $longitudeDegrees * M_PI / 180 ); } /** * Get the coordinate's latitude, measured in degrees. * * @return float */ public function latitudeDegrees() : float { return $this->latitudeRadians * 180 / M_PI; } /** * Get the coordinate's latitude, measured in radians. * * @return float */ public function latitudeRadians() : float { return $this->latitudeRadians; } /** * Get the coordinate's longitude, measured in degrees. * * @return float */ public function longitudeDegrees() : float { return $this->longitudeRadians * 180 / M_PI; } /** * Get the coordinate's longitude, measured in radians. * * @return float */ public function longitudeRadians() : float { return $this->longitudeRadians; } /** * Is this coordinate equivalent to another? * * @param static $other * * @return bool */ public function equals(self $other) : bool { return $this->latitudeRadians() === $other->latitudeRadians() && $this->longitudeRadians() === $other->longitudeRadians() ; } /** * Calculate the distance between this and another coordinate, in kilometers. * * @param static $other * * @return float */ public function distanceKilometers(self $other) : float { // Haversine formula for great-circle distance. $earthRadiusKilometers = 6371; $deltaLatitudeRadians = $this->latitudeRadians() - $other->latitudeRadians(); $deltaLongitudeRadians = $this->longitudeRadians() - $other->longitudeRadians(); $a = sin($deltaLatitudeRadians / 2) ** 2 + cos($this->latitudeRadians()) * cos($other->latitudeRadians()) * sin($deltaLongitudeRadians / 2) ** 2 ; $c = 2 * asin(sqrt($a)); return $c * $earthRadiusKilometers; } } $latitudeWorkDegrees = 40.981123; $longitudeWorkDegrees = -74.121752; $latitudeHomeDegrees = 41.184084; $longitudeHomeDegrees = -74.555852; $latitudeWorkRadians = deg2rad($latitudeWorkDegrees); $longitudeWorkRadians = deg2rad($longitudeWorkDegrees); $latitudeHomeRadians = deg2rad($latitudeHomeDegrees); $longitudeHomeRadians = deg2rad($longitudeHomeDegrees); $coordinateWork = new Coordinate($latitudeWorkRadians, $longitudeWorkRadians); $coordinateHome = new Coordinate($latitudeHomeRadians, $longitudeHomeRadians); $distance = $coordinateWork->distanceKilometers($coordinateHome); echo $distance;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jFil3
function name:  (null)
number of ops:  36
compiled vars:  !0 = $latitudeWorkDegrees, !1 = $longitudeWorkDegrees, !2 = $latitudeHomeDegrees, !3 = $longitudeHomeDegrees, !4 = $latitudeWorkRadians, !5 = $longitudeWorkRadians, !6 = $latitudeHomeRadians, !7 = $longitudeHomeRadians, !8 = $coordinateWork, !9 = $coordinateHome, !10 = $distance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  122     0  E >   ASSIGN                                                   !0, 40.9811
  123     1        ASSIGN                                                   !1, -74.1218
  124     2        ASSIGN                                                   !2, 41.1841
  125     3        ASSIGN                                                   !3, -74.5559
  127     4        INIT_FCALL                                               'deg2rad'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $15     
          7        ASSIGN                                                   !4, $15
  128     8        INIT_FCALL                                               'deg2rad'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $17     
         11        ASSIGN                                                   !5, $17
  129    12        INIT_FCALL                                               'deg2rad'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $19     
         15        ASSIGN                                                   !6, $19
  130    16        INIT_FCALL                                               'deg2rad'
         17        SEND_VAR                                                 !3
         18        DO_ICALL                                         $21     
         19        ASSIGN                                                   !7, $21
  132    20        NEW                                              $23     'Coordinate'
         21        SEND_VAR_EX                                              !4
         22        SEND_VAR_EX                                              !5
         23        DO_FCALL                                      0          
         24        ASSIGN                                                   !8, $23
  133    25        NEW                                              $26     'Coordinate'
         26        SEND_VAR_EX                                              !6
         27        SEND_VAR_EX                                              !7
         28        DO_FCALL                                      0          
         29        ASSIGN                                                   !9, $26
  135    30        INIT_METHOD_CALL                                         !8, 'distanceKilometers'
         31        SEND_VAR_EX                                              !9
         32        DO_FCALL                                      0  $29     
         33        ASSIGN                                                   !10, $29
  136    34        ECHO                                                     !10
         35      > RETURN                                                   1

Class Coordinate:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jFil3
function name:  __construct
number of ops:  7
compiled vars:  !0 = $latitudeRadians, !1 = $longitudeRadians
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     2        ASSIGN_OBJ                                               'latitudeRadians'
          3        OP_DATA                                                  !0
   22     4        ASSIGN_OBJ                                               'longitudeRadians'
          5        OP_DATA                                                  !1
   23     6      > RETURN                                                   null

End of function __construct

Function fromdegrees:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jFil3
function name:  fromDegrees
number of ops:  12
compiled vars:  !0 = $latitudeDegrees, !1 = $longitudeDegrees
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   35     2        NEW                          static              $2      
   36     3        MUL                                              ~3      !0, 3.14159
          4        DIV                                              ~4      ~3, 180
          5        SEND_VAL_EX                                              ~4
   37     6        MUL                                              ~5      !1, 3.14159
          7        DIV                                              ~6      ~5, 180
          8        SEND_VAL_EX                                              ~6
          9        DO_FCALL                                      0          
         10      > RETURN                                                   $2
   39    11*     > RETURN                                                   null

End of function fromdegrees

Function latitudedegrees:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jFil3
function name:  latitudeDegrees
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   FETCH_OBJ_R                                      ~0      'latitudeRadians'
          1        MUL                                              ~1      ~0, 180
          2        DIV                                              ~2      ~1, 3.14159
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   49     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function latitudedegrees

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

End of function latituderadians

Function longitudedegrees:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jFil3
function name:  longitudeDegrees
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   FETCH_OBJ_R                                      ~0      'longitudeRadians'
          1        MUL                                              ~1      ~0, 180
          2        DIV                                              ~2      ~1, 3.14159
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   69     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function longitudedegrees

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

End of function longituderadians

Function equals:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/jFil3
function name:  equals
number of ops:  17
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
   90     1        INIT_METHOD_CALL                                         'latitudeRadians'
          2        DO_FCALL                                      0  $1      
          3        INIT_METHOD_CALL                                         !0, 'latitudeRadians'
          4        DO_FCALL                                      0  $2      
          5        IS_IDENTICAL                                     ~3      $1, $2
          6      > JMPZ_EX                                          ~3      ~3, ->13
   91     7    >   INIT_METHOD_CALL                                         'longitudeRadians'
          8        DO_FCALL                                      0  $4      
          9        INIT_METHOD_CALL                                         !0, 'longitudeRadians'
         10        DO_FCALL                                      0  $5      
         11        IS_IDENTICAL                                     ~6      $4, $5
         12        BOOL                                             ~3      ~6
         13    >   VERIFY_RETURN_TYPE                                       ~3
         14      > RETURN                                                   ~3
   93    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function equals

Function distancekilometers:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jFil3
function name:  distanceKilometers
number of ops:  51
compiled vars:  !0 = $other, !1 = $earthRadiusKilometers, !2 = $deltaLatitudeRadians, !3 = $deltaLongitudeRadians, !4 = $a, !5 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   RECV                                             !0      
  106     1        ASSIGN                                                   !1, 6371
  107     2        INIT_METHOD_CALL                                         'latitudeRadians'
          3        DO_FCALL                                      0  $7      
          4        INIT_METHOD_CALL                                         !0, 'latitudeRadians'
          5        DO_FCALL                                      0  $8      
          6        SUB                                              ~9      $7, $8
          7        ASSIGN                                                   !2, ~9
  108     8        INIT_METHOD_CALL                                         'longitudeRadians'
          9        DO_FCALL                                      0  $11     
         10        INIT_METHOD_CALL                                         !0, 'longitudeRadians'
         11        DO_FCALL                                      0  $12     
         12        SUB                                              ~13     $11, $12
         13        ASSIGN                                                   !3, ~13
  110    14        INIT_FCALL                                               'sin'
         15        DIV                                              ~15     !2, 2
         16        SEND_VAL                                                 ~15
         17        DO_ICALL                                         $16     
         18        POW                                              ~17     $16, 2
  111    19        INIT_FCALL                                               'cos'
         20        INIT_METHOD_CALL                                         'latitudeRadians'
         21        DO_FCALL                                      0  $18     
         22        SEND_VAR                                                 $18
         23        DO_ICALL                                         $19     
  112    24        INIT_FCALL                                               'cos'
         25        INIT_METHOD_CALL                                         !0, 'latitudeRadians'
         26        DO_FCALL                                      0  $20     
         27        SEND_VAR                                                 $20
         28        DO_ICALL                                         $21     
         29        MUL                                              ~22     $19, $21
  113    30        INIT_FCALL                                               'sin'
         31        DIV                                              ~23     !3, 2
         32        SEND_VAL                                                 ~23
         33        DO_ICALL                                         $24     
         34        POW                                              ~25     $24, 2
         35        MUL                                              ~26     ~22, ~25
         36        ADD                                              ~27     ~17, ~26
  110    37        ASSIGN                                                   !4, ~27
  116    38        INIT_FCALL                                               'asin'
         39        INIT_FCALL                                               'sqrt'
         40        SEND_VAR                                                 !4
         41        DO_ICALL                                         $29     
         42        SEND_VAR                                                 $29
         43        DO_ICALL                                         $30     
         44        MUL                                              ~31     $30, 2
         45        ASSIGN                                                   !5, ~31
  118    46        MUL                                              ~33     !5, !1
         47        VERIFY_RETURN_TYPE                                       ~33
         48      > RETURN                                                   ~33
  119    49*       VERIFY_RETURN_TYPE                                       
         50*     > RETURN                                                   null

End of function distancekilometers

End of class Coordinate.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.55 ms | 1412 KiB | 23 Q