3v4l.org

run code in 300+ PHP versions simultaneously
<?php $txt = <<<TXT Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 TXT; class FileObject extends SplFileObject { #[\ReturnTypeWillChange] public function seek($offset) { if ($offset < 0) { throw new Exception("Can't seek file: " . $this->getPathname() . " to negative offset: $offset"); } if ($offset === 0) { parent::seek(0); return; } $this->rewind(); for ($i = 0; $i < $offset; $i++) { $this->fgets(); $this->next(); if ($this->eof()) { break; } } //$this->current(); } } $file = new FileObject('php://memory', 'w+'); $file->fwrite($txt); echo 'Seeking line 2...', PHP_EOL; $file->seek(1); echo 'Position in the file: ', $file->ftell(), PHP_EOL; echo 'Rest of the file: ', PHP_EOL, $file->fread(5000); echo PHP_EOL, '-------------', PHP_EOL; echo 'Seeking line last line...', PHP_EOL; $file->seek(PHP_INT_MAX); $lastLine = $file->key(); $offset = max($lastLine - 1, 0); $file->seek($offset); echo 'Position in the file: ', $file->ftell(), PHP_EOL; echo 'Rest of the file: ', PHP_EOL, $file->fread(5000);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Uhlpb
function name:  (null)
number of ops:  57
compiled vars:  !0 = $txt, !1 = $file, !2 = $lastLine, !3 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Line+1%0ALine+2%0ALine+3%0ALine+4%0ALine+5%0ALine+6%0ALine+7%0ALine+8'
   41     1        NEW                                              $5      'FileObject'
          2        SEND_VAL_EX                                              'php%3A%2F%2Fmemory'
          3        SEND_VAL_EX                                              'w%2B'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $5
   42     6        INIT_METHOD_CALL                                         !1, 'fwrite'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
   44     9        ECHO                                                     'Seeking+line+2...'
         10        ECHO                                                     '%0A'
   45    11        INIT_METHOD_CALL                                         !1, 'seek'
         12        SEND_VAL_EX                                              1
         13        DO_FCALL                                      0          
   46    14        ECHO                                                     'Position+in+the+file%3A+'
         15        INIT_METHOD_CALL                                         !1, 'ftell'
         16        DO_FCALL                                      0  $10     
         17        ECHO                                                     $10
         18        ECHO                                                     '%0A'
   47    19        ECHO                                                     'Rest+of+the+file%3A+'
         20        ECHO                                                     '%0A'
         21        INIT_METHOD_CALL                                         !1, 'fread'
         22        SEND_VAL_EX                                              5000
         23        DO_FCALL                                      0  $11     
         24        ECHO                                                     $11
   49    25        ECHO                                                     '%0A'
         26        ECHO                                                     '-------------'
         27        ECHO                                                     '%0A'
   51    28        ECHO                                                     'Seeking+line+last+line...'
         29        ECHO                                                     '%0A'
   52    30        INIT_METHOD_CALL                                         !1, 'seek'
         31        SEND_VAL_EX                                              9223372036854775807
         32        DO_FCALL                                      0          
   53    33        INIT_METHOD_CALL                                         !1, 'key'
         34        DO_FCALL                                      0  $13     
         35        ASSIGN                                                   !2, $13
   54    36        INIT_FCALL                                               'max'
         37        SUB                                              ~15     !2, 1
         38        SEND_VAL                                                 ~15
         39        SEND_VAL                                                 0
         40        DO_ICALL                                         $16     
         41        ASSIGN                                                   !3, $16
   55    42        INIT_METHOD_CALL                                         !1, 'seek'
         43        SEND_VAR_EX                                              !3
         44        DO_FCALL                                      0          
   57    45        ECHO                                                     'Position+in+the+file%3A+'
         46        INIT_METHOD_CALL                                         !1, 'ftell'
         47        DO_FCALL                                      0  $19     
         48        ECHO                                                     $19
         49        ECHO                                                     '%0A'
   58    50        ECHO                                                     'Rest+of+the+file%3A+'
         51        ECHO                                                     '%0A'
         52        INIT_METHOD_CALL                                         !1, 'fread'
         53        SEND_VAL_EX                                              5000
         54        DO_FCALL                                      0  $20     
         55        ECHO                                                     $20
         56      > RETURN                                                   1

Class FileObject:
Function seek:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 23
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 31
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 23
Branch analysis from position: 34
Branch analysis from position: 23
filename:       /in/Uhlpb
function name:  seek
number of ops:  35
compiled vars:  !0 = $offset, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   19     1        IS_SMALLER                                               !0, 0
          2      > JMPZ                                                     ~2, ->13
   20     3    >   NEW                                              $3      'Exception'
          4        INIT_METHOD_CALL                                         'getPathname'
          5        DO_FCALL                                      0  $4      
          6        CONCAT                                           ~5      'Can%27t+seek+file%3A+', $4
          7        NOP                                                      
          8        FAST_CONCAT                                      ~6      '+to+negative+offset%3A+', !0
          9        CONCAT                                           ~7      ~5, ~6
         10        SEND_VAL_EX                                              ~7
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $3
   23    13    >   IS_IDENTICAL                                             !0, 0
         14      > JMPZ                                                     ~9, ->19
   24    15    >   INIT_STATIC_METHOD_CALL                                  'seek'
         16        SEND_VAL_EX                                              0
         17        DO_FCALL                                      0          
   25    18      > RETURN                                                   null
   28    19    >   INIT_METHOD_CALL                                         'rewind'
         20        DO_FCALL                                      0          
   29    21        ASSIGN                                                   !1, 0
         22      > JMP                                                      ->32
   30    23    >   INIT_METHOD_CALL                                         'fgets'
         24        DO_FCALL                                      0          
   31    25        INIT_METHOD_CALL                                         'next'
         26        DO_FCALL                                      0          
   32    27        INIT_METHOD_CALL                                         'eof'
         28        DO_FCALL                                      0  $15     
         29      > JMPZ                                                     $15, ->31
   33    30    > > JMP                                                      ->34
   29    31    >   PRE_INC                                                  !1
         32    >   IS_SMALLER                                               !1, !0
         33      > JMPNZ                                                    ~17, ->23
   38    34    > > RETURN                                                   null

End of function seek

End of class FileObject.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
16.36 ms | 471 KiB | 7 Q