diff options
| author | Marcus Boerger <helly@php.net> | 2002-11-04 10:51:08 +0000 |
|---|---|---|
| committer | Marcus Boerger <helly@php.net> | 2002-11-04 10:51:08 +0000 |
| commit | 29372b231bce943f1308562ac44f68e261ef6b5a (patch) | |
| tree | 280c6d8a2a12e8e956896f3f48e73c8fcbdbff34 | |
| parent | a71f734dec3c76675a8dd080dab88ab0e91c44b1 (diff) | |
| download | php-git-29372b231bce943f1308562ac44f68e261ef6b5a.tar.gz | |
See http://bugs.php.net/20175
| -rw-r--r-- | tests/lang/bug20175.phpt | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/lang/bug20175.phpt b/tests/lang/bug20175.phpt new file mode 100644 index 0000000000..3b54dfda0b --- /dev/null +++ b/tests/lang/bug20175.phpt @@ -0,0 +1,85 @@ +--TEST-- +Bug #20175 (Static vars can't store ref to new instance) +--FILE-- +<?php +/* Part 1: + * Storing the result of a function in a static variable. + * foo_global() increments global variable $foo_count whenever it is executed. + * When foo_static() is called it checks for the static variable $foo_value + * being initialised. In case initialisation is necessary foo_global() will be + * called. Since that must happen only once the return value should be equal. + */ +$foo_count = 0; + +function foo_global() { + global $foo_count; + return 'foo:' . ++$foo_count; +} + +function foo_static() { + static $foo_value; + if (!isset($foo_value)) { + $foo_value = foo_global(); + } + return $foo_value; +} + +/* Part 2: + * Storing a reference to the result of a function in a static variable. + * Same as Part 1 but: + * The return statment transports a copy of the value to return. In other + * words the return value of bar_global() is a temporary variable only valid + * after the function call bar_global() is done in current local scope. + */ +$bar_global = 0; + +function bar_global() { + global $bar_count; + return 'bar:' . ++$bar_count; +} + +function bar_static() { + static $bar_value; + if (!isset($bar_value)) { + $bar_value = &bar_global(); + } + return $bar_value; +} + +/* Part 3: + * Storing a reference to the result of a function in a static variable. + * Same as Part 2 but wow_global() returns a reference. + */ +$wow_global = 0; +$wow_name = ''; + +function &wow_global() { + global $wow_count, $wow_name; + $wow_name = 'wow:' . ++$wow_count; + return $wow_name; +} + +function wow_static() { + static $wow_value; + if (!isset($wow_value)) { + $wow_value = &wow_global(); + } + return $wow_value; +} + +print zend_version()."\n"; +print foo_static()."\n"; +print foo_static()."\n"; +print bar_static()."\n"; +print bar_static()."\n"; +print wow_static()."\n"; +print wow_static()."\n"; +?> +--EXPECTF-- +%s +foo:1 +foo:1 +bar:1 +bar:2 +wow:1 +wow:1 |
