xref: /PHP-7.1/ext/standard/tests/file/bug38450.phpt (revision 7af945e2)
1--TEST--
2Bug #38450 (constructor is not called for classes used in userspace stream wrappers)
3--FILE--
4<?php
5
6class VariableStream {
7	var $position;
8	var $varname;
9
10	function __construct($var=null) {
11		var_dump("constructor!");
12	}
13
14	function stream_open($path, $mode, $options, &$opened_path)
15	{
16		$url = parse_url($path);
17		$this->varname = $url["host"];
18		$this->position = 0;
19
20		return true;
21	}
22
23	function stream_read($count)
24	{
25		$ret = substr($GLOBALS[$this->varname], $this->position, $count);
26		$this->position += strlen($ret);
27		return $ret;
28	}
29
30	function stream_write($data)
31	{
32		$left = substr($GLOBALS[$this->varname], 0, $this->position);
33		$right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
34		$GLOBALS[$this->varname] = $left . $data . $right;
35		$this->position += strlen($data);
36		return strlen($data);
37	}
38
39	function stream_tell()
40	{
41		return $this->position;
42	}
43
44	function stream_eof()
45	{
46		return $this->position >= strlen($GLOBALS[$this->varname]);
47	}
48	function stream_seek($offset, $whence)
49	{
50		switch ($whence) {
51		case SEEK_SET:
52			if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
53				$this->position = $offset;
54				return true;
55			} else {
56				return false;
57			}
58			break;
59
60		case SEEK_CUR:
61			if ($offset >= 0) {
62				$this->position += $offset;
63				return true;
64			} else {
65				return false;
66			}
67			break;
68
69		case SEEK_END:
70			if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
71				$this->position = strlen($GLOBALS[$this->varname]) + $offset;
72				return true;
73			} else {
74				return false;
75			}
76			break;
77
78		default:
79			return false;
80		}
81	}
82}
83
84stream_wrapper_register("var", "VariableStream")
85	or die("Failed to register protocol");
86
87$myvar = "";
88
89$fp = fopen("var://myvar", "r+");
90
91fwrite($fp, b"line1\n");
92fwrite($fp, b"line2\n");
93fwrite($fp, b"line3\n");
94
95rewind($fp);
96while (!feof($fp)) {
97	echo fgets($fp);
98}
99fclose($fp);
100var_dump($myvar);
101
102echo "Done\n";
103?>
104--EXPECTF--
105string(12) "constructor!"
106line1
107line2
108line3
109string(18) "line1
110line2
111line3
112"
113Done
114