Example 1 — Register a Tiny Read Wrapper
Register a simple wrapper, open it, read fixed text, then unregister.
<?php
class TinyWrap {
public $context;
private $data = "hello";
private $pos = 0;
public function stream_open($path, $mode, $options, &$opened_path) {
return true;
}
public function stream_read($count) {
$chunk = substr($this->data, $this->pos, $count);
$this->pos += strlen($chunk);
return $chunk;
}
public function stream_eof() {
return $this->pos >= strlen($this->data);
}
public function stream_stat() {
return [];
}
}
stream_register_wrapper("tiny", TinyWrap::class);
$fp = fopen("tiny://x", "r");
echo stream_get_contents($fp), PHP_EOL;
fclose($fp);
stream_wrapper_unregister("tiny"); How It Works
- TinyWrap implements the minimum methods for a readable wrapper.
- stream_register_wrapper() links the protocol tiny to that class.
- fopen("tiny://x") creates an instance and stream_get_contents() reads hello.
- stream_wrapper_unregister() removes the custom protocol afterward.

