3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Implements a read/write buffer. */ class Buffer { protected $eof = false; protected $indexRead = 0; protected $indexWrite = 0; protected $buffer = []; public function write($bytes) { if ($bytes === null) { $this->eof = true; } else { $this->buffer[$this->indexWrite++] = $bytes; } } public function read($length = 4096) { if (!$this->buffer) { return $this->eof ? null : ''; } $output = ''; $outputLength = 0; while ($this->indexRead < $this->indexWrite) { $chunk = $this->buffer[$this->indexRead]; $chunkLength = \strlen($chunk); if ($outputLength + $chunkLength == $length) { unset($this->buffer[$this->indexRead++]); $output .= $chunk; return $output; } if ($outputLength + $chunkLength > $length) { $slice = $length - $outputLength; $this->buffer[$this->indexRead] = \substr($chunk, $slice); $output .= \substr($chunk, 0, $slice); return $output; } unset($this->buffer[$this->indexRead++]); $output .= $chunk; $outputLength += $chunkLength; } return $output; } } $b = new Buffer(); $b->write('ABCDEFGHIJ'); $b->write('KLMNOPQRSTUVWXYZ'); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); echo 'Writing more...' . PHP_EOL; $b->write('01234567890'); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); echo 'EOF...' . PHP_EOL; $b->write(null); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hq4L2
function name:  (null)
number of ops:  168
compiled vars:  !0 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   NEW                                              $1      'Buffer'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   55     3        INIT_METHOD_CALL                                         !0, 'write'
          4        SEND_VAL_EX                                              'ABCDEFGHIJ'
          5        DO_FCALL                                      0          
   56     6        INIT_METHOD_CALL                                         !0, 'write'
          7        SEND_VAL_EX                                              'KLMNOPQRSTUVWXYZ'
          8        DO_FCALL                                      0          
   58     9        INIT_FCALL                                               'var_dump'
         10        INIT_METHOD_CALL                                         !0, 'read'
         11        SEND_VAL_EX                                              3
         12        DO_FCALL                                      0  $6      
         13        SEND_VAR                                                 $6
         14        DO_ICALL                                                 
   59    15        INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         !0, 'read'
         17        SEND_VAL_EX                                              3
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                                 
   60    21        INIT_FCALL                                               'var_dump'
         22        INIT_METHOD_CALL                                         !0, 'read'
         23        SEND_VAL_EX                                              3
         24        DO_FCALL                                      0  $10     
         25        SEND_VAR                                                 $10
         26        DO_ICALL                                                 
   61    27        INIT_FCALL                                               'var_dump'
         28        INIT_METHOD_CALL                                         !0, 'read'
         29        SEND_VAL_EX                                              3
         30        DO_FCALL                                      0  $12     
         31        SEND_VAR                                                 $12
         32        DO_ICALL                                                 
   62    33        INIT_FCALL                                               'var_dump'
         34        INIT_METHOD_CALL                                         !0, 'read'
         35        SEND_VAL_EX                                              3
         36        DO_FCALL                                      0  $14     
         37        SEND_VAR                                                 $14
         38        DO_ICALL                                                 
   63    39        INIT_FCALL                                               'var_dump'
         40        INIT_METHOD_CALL                                         !0, 'read'
         41        SEND_VAL_EX                                              3
         42        DO_FCALL                                      0  $16     
         43        SEND_VAR                                                 $16
         44        DO_ICALL                                                 
   64    45        INIT_FCALL                                               'var_dump'
         46        INIT_METHOD_CALL                                         !0, 'read'
         47        SEND_VAL_EX                                              3
         48        DO_FCALL                                      0  $18     
         49        SEND_VAR                                                 $18
         50        DO_ICALL                                                 
   65    51        INIT_FCALL                                               'var_dump'
         52        INIT_METHOD_CALL                                         !0, 'read'
         53        SEND_VAL_EX                                              3
         54        DO_FCALL                                      0  $20     
         55        SEND_VAR                                                 $20
         56        DO_ICALL                                                 
   66    57        INIT_FCALL                                               'var_dump'
         58        INIT_METHOD_CALL                                         !0, 'read'
         59        SEND_VAL_EX                                              3
         60        DO_FCALL                                      0  $22     
         61        SEND_VAR                                                 $22
         62        DO_ICALL                                                 
   67    63        INIT_FCALL                                               'var_dump'
         64        INIT_METHOD_CALL                                         !0, 'read'
         65        SEND_VAL_EX                                              3
         66        DO_FCALL                                      0  $24     
         67        SEND_VAR                                                 $24
         68        DO_ICALL                                                 
   68    69        INIT_FCALL                                               'var_dump'
         70        INIT_METHOD_CALL                                         !0, 'read'
         71        SEND_VAL_EX                                              3
         72        DO_FCALL                                      0  $26     
         73        SEND_VAR                                                 $26
         74        DO_ICALL                                                 
   69    75        INIT_FCALL                                               'var_dump'
         76        INIT_METHOD_CALL                                         !0, 'read'
         77        SEND_VAL_EX                                              3
         78        DO_FCALL                                      0  $28     
         79        SEND_VAR                                                 $28
         80        DO_ICALL                                                 
   70    81        INIT_FCALL                                               'var_dump'
         82        INIT_METHOD_CALL                                         !0, 'read'
         83        SEND_VAL_EX                                              3
         84        DO_FCALL                                      0  $30     
         85        SEND_VAR                                                 $30
         86        DO_ICALL                                                 
   71    87        INIT_FCALL                                               'var_dump'
         88        INIT_METHOD_CALL                                         !0, 'read'
         89        SEND_VAL_EX                                              3
         90        DO_FCALL                                      0  $32     
         91        SEND_VAR                                                 $32
         92        DO_ICALL                                                 
   72    93        INIT_FCALL                                               'var_dump'
         94        INIT_METHOD_CALL                                         !0, 'read'
         95        SEND_VAL_EX                                              3
         96        DO_FCALL                                      0  $34     
         97        SEND_VAR                                                 $34
         98        DO_ICALL                                                 
   73    99        INIT_FCALL                                               'var_dump'
        100        INIT_METHOD_CALL                                         !0, 'read'
        101        SEND_VAL_EX                                              3
        102        DO_FCALL                                      0  $36     
        103        SEND_VAR                                                 $36
        104        DO_ICALL                                                 
   74   105        INIT_FCALL                                               'var_dump'
        106        INIT_METHOD_CALL                                         !0, 'read'
        107        SEND_VAL_EX                                              3
        108        DO_FCALL                                      0  $38     
        109        SEND_VAR                                                 $38
        110        DO_ICALL                                                 
   76   111        ECHO                                                     'Writing+more...%0A'
   77   112        INIT_METHOD_CALL                                         !0, 'write'
        113        SEND_VAL_EX                                              '01234567890'
        114        DO_FCALL                                      0          
   78   115        INIT_FCALL                                               'var_dump'
        116        INIT_METHOD_CALL                                         !0, 'read'
        117        SEND_VAL_EX                                              3
        118        DO_FCALL                                      0  $41     
        119        SEND_VAR                                                 $41
        120        DO_ICALL                                                 
   79   121        INIT_FCALL                                               'var_dump'
        122        INIT_METHOD_CALL                                         !0, 'read'
        123        SEND_VAL_EX                                              3
        124        DO_FCALL                                      0  $43     
        125        SEND_VAR                                                 $43
        126        DO_ICALL                                                 
   80   127        INIT_FCALL                                               'var_dump'
        128        INIT_METHOD_CALL                                         !0, 'read'
        129        SEND_VAL_EX                                              3
        130        DO_FCALL                                      0  $45     
        131        SEND_VAR                                                 $45
        132        DO_ICALL                                                 
   81   133        INIT_FCALL                                               'var_dump'
        134        INIT_METHOD_CALL                                         !0, 'read'
        135        SEND_VAL_EX                                              3
        136        DO_FCALL                                      0  $47     
        137        SEND_VAR                                                 $47
        138        DO_ICALL                                                 
   82   139        INIT_FCALL                                               'var_dump'
        140        INIT_METHOD_CALL                                         !0, 'read'
        141        SEND_VAL_EX                                              3
        142        DO_FCALL                                      0  $49     
        143        SEND_VAR                                                 $49
        144        DO_ICALL                                                 
   84   145        ECHO                                                     'EOF...%0A'
   85   146        INIT_METHOD_CALL                                         !0, 'write'
        147        SEND_VAL_EX                                              null
        148        DO_FCALL                                      0          
   86   149        INIT_FCALL                                               'var_dump'
        150        INIT_METHOD_CALL                                         !0, 'read'
        151        SEND_VAL_EX                                              3
        152        DO_FCALL                                      0  $52     
        153        SEND_VAR                                                 $52
        154        DO_ICALL                                                 
   87   155        INIT_FCALL                                               'var_dump'
        156        INIT_METHOD_CALL                                         !0, 'read'
        157        SEND_VAL_EX                                              3
        158        DO_FCALL                                      0  $54     
        159        SEND_VAR                                                 $54
        160        DO_ICALL                                                 
   88   161        INIT_FCALL                                               'var_dump'
        162        INIT_METHOD_CALL                                         !0, 'read'
        163        SEND_VAL_EX                                              3
        164        DO_FCALL                                      0  $56     
        165        SEND_VAR                                                 $56
        166        DO_ICALL                                                 
        167      > RETURN                                                   1

Class Buffer:
Function write:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hq4L2
function name:  write
number of ops:  11
compiled vars:  !0 = $bytes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   12     1        TYPE_CHECK                                    2          !0
          2      > JMPZ                                                     ~1, ->6
   13     3    >   ASSIGN_OBJ                                               'eof'
          4        OP_DATA                                                  <true>
          5      > JMP                                                      ->10
   15     6    >   POST_INC_OBJ                                     ~4      'indexWrite'
          7        FETCH_OBJ_W                                      $3      'buffer'
          8        ASSIGN_DIM                                               $3, ~4
          9        OP_DATA                                                  !0
   17    10    > > RETURN                                                   null

End of function write

Function read:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 13
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 27
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 47
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 13
Branch analysis from position: 56
Branch analysis from position: 13
filename:       /in/hq4L2
function name:  read
number of ops:  58
compiled vars:  !0 = $length, !1 = $output, !2 = $outputLength, !3 = $chunk, !4 = $chunkLength, !5 = $slice
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV_INIT                                        !0      4096
   20     1        FETCH_OBJ_R                                      ~6      'buffer'
          2        BOOL_NOT                                         ~7      ~6
          3      > JMPZ                                                     ~7, ->10
   21     4    >   FETCH_OBJ_R                                      ~8      'eof'
          5      > JMPZ                                                     ~8, ->8
          6    >   QM_ASSIGN                                        ~9      null
          7      > JMP                                                      ->9
          8    >   QM_ASSIGN                                        ~9      ''
          9    > > RETURN                                                   ~9
   24    10    >   ASSIGN                                                   !1, ''
   25    11        ASSIGN                                                   !2, 0
   26    12      > JMP                                                      ->52
   27    13    >   FETCH_OBJ_R                                      ~13     'indexRead'
         14        FETCH_OBJ_R                                      ~12     'buffer'
         15        FETCH_DIM_R                                      ~14     ~12, ~13
         16        ASSIGN                                                   !3, ~14
   28    17        STRLEN                                           ~16     !3
         18        ASSIGN                                                   !4, ~16
   30    19        ADD                                              ~18     !2, !4
         20        IS_EQUAL                                                 !0, ~18
         21      > JMPZ                                                     ~19, ->27
   31    22    >   POST_INC_OBJ                                     ~21     'indexRead'
         23        FETCH_OBJ_UNSET                                  $20     'buffer'
         24        UNSET_DIM                                                $20, ~21
   32    25        ASSIGN_OP                                     8          !1, !3
   33    26      > RETURN                                                   !1
   36    27    >   ADD                                              ~23     !2, !4
         28        IS_SMALLER                                               !0, ~23
         29      > JMPZ                                                     ~24, ->47
   37    30    >   SUB                                              ~25     !0, !2
         31        ASSIGN                                                   !5, ~25
   38    32        FETCH_OBJ_R                                      ~28     'indexRead'
         33        INIT_FCALL                                               'substr'
         34        SEND_VAR                                                 !3
         35        SEND_VAR                                                 !5
         36        DO_ICALL                                         $30     
         37        FETCH_OBJ_W                                      $27     'buffer'
         38        ASSIGN_DIM                                               $27, ~28
         39        OP_DATA                                                  $30
   39    40        INIT_FCALL                                               'substr'
         41        SEND_VAR                                                 !3
         42        SEND_VAL                                                 0
         43        SEND_VAR                                                 !5
         44        DO_ICALL                                         $31     
         45        ASSIGN_OP                                     8          !1, $31
   40    46      > RETURN                                                   !1
   43    47    >   POST_INC_OBJ                                     ~34     'indexRead'
         48        FETCH_OBJ_UNSET                                  $33     'buffer'
         49        UNSET_DIM                                                $33, ~34
   44    50        ASSIGN_OP                                     8          !1, !3
   45    51        ASSIGN_OP                                     1          !2, !4
   26    52    >   FETCH_OBJ_R                                      ~37     'indexRead'
         53        FETCH_OBJ_R                                      ~38     'indexWrite'
         54        IS_SMALLER                                               ~37, ~38
         55      > JMPNZ                                                    ~39, ->13
   48    56    > > RETURN                                                   !1
   49    57*     > RETURN                                                   null

End of function read

End of class Buffer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.9 ms | 1416 KiB | 17 Q