xref: /PHP-5.3/tests/lang/bug20175.phpt (revision d3e50265)
1--TEST--
2Bug #20175 (Static vars can't store ref to new instance)
3--SKIPIF--
4<?php if (version_compare(zend_version(),'2.0.0-dev','<')) die('skip ZE1 does not have static class members'); ?>
5--INI--
6error_reporting=E_ALL | E_STRICT | E_DEPRECATED
7--FILE--
8<?php
9print zend_version()."\n";
10
11/* Part 1:
12 * Storing the result of a function in a static variable.
13 * foo_global() increments global variable $foo_count whenever it is executed.
14 * When foo_static() is called it checks for the static variable $foo_value
15 * being initialised. In case initialisation is necessary foo_global() will be
16 * called. Since that must happen only once the return value should be equal.
17 */
18$foo_count = 0;
19
20function foo_global() {
21	global $foo_count;
22	echo "foo_global()\n";
23	return 'foo:' . ++$foo_count;
24}
25
26function foo_static() {
27	static $foo_value;
28	echo "foo_static()\n";
29	if (!isset($foo_value)) {
30		$foo_value = foo_global();
31	}
32	return $foo_value;
33}
34
35/* Part 2:
36 * Storing a reference to the result of a function in a static variable.
37 * Same as Part 1 but:
38 * The return statment transports a copy of the value to return. In other
39 * words the return value of bar_global() is a temporary variable only valid
40 * after the function call bar_global() is done in current local scope.
41 */
42$bar_count = 0;
43
44function bar_global() {
45	global $bar_count;
46	echo "bar_global()\n";
47	return 'bar:' . ++$bar_count;
48}
49
50function bar_static() {
51	static $bar_value;
52	echo "bar_static()\n";
53	if (!isset($bar_value)) {
54		$bar_value = &bar_global();
55	}
56	return $bar_value;
57}
58
59/* Part 3: TO BE DISCUSSED
60 *
61 * Storing a reference to the result of a function in a static variable.
62 * Same as Part 2 but wow_global() returns a reference so $wow_value
63 * should store a reference to $wow_global. Therefor $wow_value is already
64 * initialised in second call to wow_static() and hence shouldn't call
65 * wow_global() again.
66 */ /*
67$wow_count = 0;
68$wow_name = '';
69
70function &wow_global() {
71	global $wow_count, $wow_name;
72	echo "wow_global()\n";
73	$wow_name = 'wow:' . ++$wow_count;
74	return $wow_name;
75}
76
77function wow_static() {
78	static $wow_value;
79	echo "wow_static()\n";
80	if (!isset($wow_value)) {
81		$wow_value = &wow_global();
82	}
83	return $wow_value;
84}*/
85
86/* Part 4:
87 * Storing a reference to a new instance (that's where the name of the  test
88 * comes from). First there is the global counter $oop_global again which
89 * counts the calls to the constructor of oop_class and hence counts the
90 * creation of oop_class instances.
91 * The class oop_test uses a static reference to a oop_class instance.
92 * When another oop_test instance is created it must reuse the statically
93 * stored reference oop_value. This way oop_class gets some singleton behavior
94 * since it will be created only once for all insatnces of oop_test.
95 */
96$oop_global = 0;
97class oop_class {
98	var $oop_name;
99
100	function oop_class() {
101		global $oop_global;
102		echo "oop_class()\n";
103		$this->oop_name = 'oop:' . ++$oop_global;
104	}
105}
106
107class oop_test {
108	static $oop_value;
109
110	function oop_test() {
111		echo "oop_test()\n";
112	}
113
114	function oop_static() {
115		echo "oop_static()\n";
116		if (!isset(self::$oop_value)) {
117			self::$oop_value = & new oop_class;
118		}
119		echo self::$oop_value->oop_name;
120	}
121}
122
123print foo_static()."\n";
124print foo_static()."\n";
125print bar_static()."\n";
126print bar_static()."\n";
127//print wow_static()."\n";
128//print wow_static()."\n";
129echo "wow_static()
130wow_global()
131wow:1
132wow_static()
133wow:1
134";
135$oop_tester = new oop_test;
136print $oop_tester->oop_static()."\n";
137print $oop_tester->oop_static()."\n";
138$oop_tester = new oop_test; // repeated.
139print $oop_tester->oop_static()."\n";
140?>
141--EXPECTF--
142Deprecated: Assigning the return value of new by reference is deprecated in %s.php on line %d
143%s
144foo_static()
145foo_global()
146foo:1
147foo_static()
148foo:1
149bar_static()
150bar_global()
151
152Strict Standards: Only variables should be assigned by reference in %sbug20175.php on line 47
153bar:1
154bar_static()
155bar:1
156wow_static()
157wow_global()
158wow:1
159wow_static()
160wow:1
161oop_test()
162oop_static()
163oop_class()
164oop:1
165oop_static()
166oop:1
167oop_test()
168oop_static()
169oop:1
170