note 77057 added to function.stream-wrapper-register
| From: | fordiman at gmail dot com | Date: | Mon, 13 Aug 2007 12:59:41 +0000 |
| Subject: | note 77057 added to function.stream-wrapper-register | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129442@lists.php.net to get a copy of this message | ||
Updated. I figured there's no need to store the variable name if we're dereferenceing; we
can just store the pointer and not have to dereference in each function for brevity.
Also, I added the assertion that the stream is a string, since we're behaving basically like it
has to be, and I changed the name to GlobalStream and global://, as that's a more descriptive
moniker than VariableName/var://.
<?php
class GlobalStream {
private $pos;
private $stream;
public function stream_open($path, $mode, $options, &$opened_path) {
$url = parse_url($path);
$this->stream = &$GLOBALS[$url["host"]];
$this->pos = 0;
if (!is_string($this->stream)) return false;
return true;
}
public function stream_read($count) {
$p=&$this->pos;
$ret = substr($this->stream, $this->pos, $count);
$this->pos += strlen($ret);
return $ret;
}
public function stream_write($data){
$l=strlen($data);
$this->stream =
substr($this->stream, 0, $this->pos) .
$data .
substr($this->stream, $this->pos += $l);
return $l;
}
public function stream_tell() {
return $this->pos;
}
public function stream_eof() {
return $this->pos >= strlen($this->stream);
}
public function stream_seek($offset, $whence) {
$l=strlen($this->stream);
switch ($whence) {
case SEEK_SET: $newPos = $offset; break;
case SEEK_CUR: $newPos = $this->pos + $offset; break;
case SEEK_END: $newPos = $l + $offset; break;
default: return false;
}
$ret = ($newPos >=0 && $newPos <=$l);
if ($ret) $this->pos=$newPos;
return $ret;
}
}
stream_wrapper_register('global', 'GlobalStream') or die('Failed to
register protocol global://');
$myvar = "";
$fp = fopen("global://myvar", "r+");
fwrite($fp, "line1\n");
fwrite($fp, "line2\n");
fwrite($fp, "line3\n");
rewind($fp);
while (!feof($fp)) {
echo fgets($fp);
}
fclose($fp);
var_dump($myvar);
?>
----
Server IP: 64.71.164.2
Probable Submitter: 141.152.239.249
----
Manual Page -- http://www.php.net/manual/en/function.stream-wrapper-register.php
Edit -- https://master.php.net/note/edit/77057
Del: integrated -- https://master.php.net/note/delete/77057/integrated
Del: useless -- https://master.php.net/note/delete/77057/useless
Del: bad code -- https://master.php.net/note/delete/77057/bad+code
Del: spam -- https://master.php.net/note/delete/77057/spam
Del: non-english -- https://master.php.net/note/delete/77057/non-english
Del: in docs -- https://master.php.net/note/delete/77057/in+docs
Del: other reasons-- https://master.php.net/note/delete/77057
Reject -- https://master.php.net/note/reject/77057
Search -- https://master.php.net/manage/user-notes.php