3v4l.org

run code in 300+ PHP versions simultaneously
<?php Class MyCalendar { public $free = array(); public $booked = array(); public function __construct($week = 'now') { $start = new DateTime($week); $start->modify('Monday this week midnight'); $end = clone $start; $end->modify('Friday this week midnight'); $interval = new DateInterval('P1D'); foreach (new DatePeriod($start, $interval, $end) as $freeTime) { $this->free[] = $freeTime; } } public function bookAppointment(DateTime $date, $note) { $this->booked[] = array('date' => $date->modify('midnight'), 'note' => $note); } public function checkAvailability() { return array_udiff($this->free, $this->booked, array($this, 'customCompare')); } public function customCompare($free, $booked) { if (is_array($free)) $a = $free['date']; else $a = $free; if (is_array($booked)) $b = $booked['date']; else $b = $booked; if ($a == $b) { return 0; } elseif ($a > $b) { return 1; } else { return -1; } } } // Create a calendar for weekly appointments $myCalendar = new MyCalendar; // Book some appointments for this week $myCalendar->bookAppointment(new DateTime('Monday this week'), "Cleaning GoogleGuy's apartment."); $myCalendar->bookAppointment(new DateTime('Wednesday this week'), "Going on a snowboarding trip."); $myCalendar->bookAppointment(new DateTime('Friday this week'), "Fixing buggy code."); // Check availability of days by comparing $booked dates against $free dates echo "I'm available on the following days this week...\n\n"; foreach ($myCalendar->checkAvailability() as $free) { echo $free->format('l'), "\n"; } echo "\n\n"; echo "I'm busy on the following days this week...\n\n"; foreach ($myCalendar->booked as $booked) { echo $booked['date']->format('l'), ": ", $booked['note'], "\n"; } ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 35
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 35
2 jumps found. (Code = 77) Position 1 = 40, Position 2 = 51
Branch analysis from position: 40
2 jumps found. (Code = 78) Position 1 = 41, Position 2 = 51
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 35
filename:       /in/efGi3
function name:  (null)
number of ops:  53
compiled vars:  !0 = $myCalendar, !1 = $free, !2 = $booked
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   NEW                                              $3      'MyCalendar'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   44     3        INIT_METHOD_CALL                                         !0, 'bookAppointment'
          4        NEW                                              $6      'DateTime'
          5        SEND_VAL_EX                                              'Monday+this+week'
          6        DO_FCALL                                      0          
          7        SEND_VAR_NO_REF_EX                                       $6
          8        SEND_VAL_EX                                              'Cleaning+GoogleGuy%27s+apartment.'
          9        DO_FCALL                                      0          
   45    10        INIT_METHOD_CALL                                         !0, 'bookAppointment'
         11        NEW                                              $9      'DateTime'
         12        SEND_VAL_EX                                              'Wednesday+this+week'
         13        DO_FCALL                                      0          
         14        SEND_VAR_NO_REF_EX                                       $9
         15        SEND_VAL_EX                                              'Going+on+a+snowboarding+trip.'
         16        DO_FCALL                                      0          
   46    17        INIT_METHOD_CALL                                         !0, 'bookAppointment'
         18        NEW                                              $12     'DateTime'
         19        SEND_VAL_EX                                              'Friday+this+week'
         20        DO_FCALL                                      0          
         21        SEND_VAR_NO_REF_EX                                       $12
         22        SEND_VAL_EX                                              'Fixing+buggy+code.'
         23        DO_FCALL                                      0          
   49    24        ECHO                                                     'I%27m+available+on+the+following+days+this+week...%0A%0A'
   50    25        INIT_METHOD_CALL                                         !0, 'checkAvailability'
         26        DO_FCALL                                      0  $15     
         27      > FE_RESET_R                                       $16     $15, ->35
         28    > > FE_FETCH_R                                               $16, !1, ->35
   51    29    >   INIT_METHOD_CALL                                         !1, 'format'
         30        SEND_VAL_EX                                              'l'
         31        DO_FCALL                                      0  $17     
         32        ECHO                                                     $17
         33        ECHO                                                     '%0A'
   50    34      > JMP                                                      ->28
         35    >   FE_FREE                                                  $16
   53    36        ECHO                                                     '%0A%0A'
   54    37        ECHO                                                     'I%27m+busy+on+the+following+days+this+week...%0A%0A'
   55    38        FETCH_OBJ_R                                      ~18     !0, 'booked'
         39      > FE_RESET_R                                       $19     ~18, ->51
         40    > > FE_FETCH_R                                               $19, !2, ->51
   56    41    >   FETCH_DIM_R                                      ~20     !2, 'date'
         42        INIT_METHOD_CALL                                         ~20, 'format'
         43        SEND_VAL_EX                                              'l'
         44        DO_FCALL                                      0  $21     
         45        ECHO                                                     $21
         46        ECHO                                                     '%3A+'
         47        FETCH_DIM_R                                      ~22     !2, 'note'
         48        ECHO                                                     ~22
         49        ECHO                                                     '%0A'
   55    50      > JMP                                                      ->40
         51    >   FE_FREE                                                  $19
   58    52      > RETURN                                                   1

Class MyCalendar:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 28
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/efGi3
function name:  __construct
number of ops:  30
compiled vars:  !0 = $week, !1 = $start, !2 = $end, !3 = $interval, !4 = $freeTime
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV_INIT                                        !0      'now'
    7     1        NEW                                              $5      'DateTime'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $5
    8     5        INIT_METHOD_CALL                                         !1, 'modify'
          6        SEND_VAL_EX                                              'Monday+this+week+midnight'
          7        DO_FCALL                                      0          
    9     8        CLONE                                            ~9      !1
          9        ASSIGN                                                   !2, ~9
   10    10        INIT_METHOD_CALL                                         !2, 'modify'
         11        SEND_VAL_EX                                              'Friday+this+week+midnight'
         12        DO_FCALL                                      0          
   11    13        NEW                                              $12     'DateInterval'
         14        SEND_VAL_EX                                              'P1D'
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !3, $12
   12    17        NEW                                              $15     'DatePeriod'
         18        SEND_VAR_EX                                              !1
         19        SEND_VAR_EX                                              !3
         20        SEND_VAR_EX                                              !2
         21        DO_FCALL                                      0          
         22      > FE_RESET_R                                       $17     $15, ->28
         23    > > FE_FETCH_R                                               $17, !4, ->28
   13    24    >   FETCH_OBJ_W                                      $18     'free'
         25        ASSIGN_DIM                                               $18
         26        OP_DATA                                                  !4
   12    27      > JMP                                                      ->23
         28    >   FE_FREE                                                  $17
   15    29      > RETURN                                                   null

End of function __construct

Function bookappointment:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/efGi3
function name:  bookAppointment
number of ops:  11
compiled vars:  !0 = $date, !1 = $note
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        INIT_METHOD_CALL                                         !0, 'modify'
          3        SEND_VAL_EX                                              'midnight'
          4        DO_FCALL                                      0  $4      
          5        INIT_ARRAY                                       ~5      $4, 'date'
          6        ADD_ARRAY_ELEMENT                                ~5      !1, 'note'
          7        FETCH_OBJ_W                                      $2      'booked'
          8        ASSIGN_DIM                                               $2
          9        OP_DATA                                                  ~5
   19    10      > RETURN                                                   null

End of function bookappointment

Function checkavailability:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/efGi3
function name:  checkAvailability
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   INIT_FCALL                                               'array_udiff'
          1        FETCH_OBJ_R                                      ~0      'free'
          2        SEND_VAL                                                 ~0
          3        FETCH_OBJ_R                                      ~1      'booked'
          4        SEND_VAL                                                 ~1
          5        FETCH_THIS                                       ~2      
          6        INIT_ARRAY                                       ~3      ~2
          7        ADD_ARRAY_ELEMENT                                ~3      'customCompare'
          8        SEND_VAL                                                 ~3
          9        DO_ICALL                                         $4      
         10      > RETURN                                                   $4
   23    11*     > RETURN                                                   null

End of function checkavailability

Function customcompare:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
Branch analysis from position: 18
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
Branch analysis from position: 13
filename:       /in/efGi3
function name:  customCompare
number of ops:  24
compiled vars:  !0 = $free, !1 = $booked, !2 = $a, !3 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        TYPE_CHECK                                  128          !0
          3      > JMPZ                                                     ~4, ->7
          4    >   FETCH_DIM_R                                      ~5      !0, 'date'
          5        ASSIGN                                                   !2, ~5
          6      > JMP                                                      ->8
   27     7    >   ASSIGN                                                   !2, !0
   28     8    >   TYPE_CHECK                                  128          !1
          9      > JMPZ                                                     ~8, ->13
         10    >   FETCH_DIM_R                                      ~9      !1, 'date'
         11        ASSIGN                                                   !3, ~9
         12      > JMP                                                      ->14
   29    13    >   ASSIGN                                                   !3, !1
   30    14    >   IS_EQUAL                                                 !2, !3
         15      > JMPZ                                                     ~12, ->18
   31    16    > > RETURN                                                   0
         17*       JMP                                                      ->23
   32    18    >   IS_SMALLER                                               !3, !2
         19      > JMPZ                                                     ~13, ->22
   33    20    > > RETURN                                                   1
         21*       JMP                                                      ->23
   35    22    > > RETURN                                                   -1
   37    23*     > RETURN                                                   null

End of function customcompare

End of class MyCalendar.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.51 ms | 1408 KiB | 15 Q