3v4l.org

run code in 300+ PHP versions simultaneously
<?php class File_stream { static $stream; public $context; private $_position; public function __get($name) { $n = "_" . $name; echo " get {$name} = {$this->$n}\n"; return $this->$n; } public function __set($name, $value) { $n = "_" . $name; echo " set {$name} = {$value}\n"; $this->$n = $value; } function stream_open ($path, $mode) { echo " open\n"; if ($mode != 'r' && $mode != 'rb') { return false; } $this->position = 0; return true; } function stream_read ($length) { echo " reading {$length} at {$this->position}\n"; fseek(self::$stream, $this->position); $bytes = fread(self::$stream, $length); $this->position = $this->position + strlen($bytes); echo " read ", strlen($bytes), " bytes\n"; return $bytes; } function stream_tell () { echo " tell\n"; return $this->position; } function stream_eof () { echo " eof\n"; fseek(self::$stream, $this->position); return feof(self::$stream); } function stream_seek ($offset, $whence = SEEK_SET) { echo " seek from {$this->position} to {$offset}\n"; fseek(self::$stream, $this->position); $result = fseek(self::$stream, $offset, $whence); $this->position = ftell(self::$stream); echo " new position is {$this->position}\n"; return $result; } function stream_stat () { echo " stat\n"; return fstat(self::$stream); } } stream_wrapper_register('request-file', 'File_stream'); $data = '0123456789abcdef'; $stream_direct = fopen('php://temp', 'w+b'); fwrite($stream_direct, $data); rewind($stream_direct); File_stream::$stream = $stream_direct; $stream = fopen('request-file://', 'r'); echo "read 3 =\n", fread($stream, 3), "\n"; echo "tell =\n", ftell($stream), "\n"; echo "seek -1\n"; fseek($stream, -1, SEEK_CUR); echo "tell =\n", ftell($stream), "\n"; echo "read 2 =\n", fread($stream, 2), "\n"; echo "tell =\n", ftell($stream), "\n"; fclose($stream);

preferences:
58.03 ms | 402 KiB | 5 Q