We now treat static variables like special variables such as $GLOBALS, $this and so on where they are treated at compile-time. This means that they are faster now but they can't be referenced indirectly similar to the above mentioned. I don't think this is a problem because static's couldn't be defined indirectly anyway. It should be documented as a change from ZE1->ZE2.
This now only leaves global with this behavior. I suggest not to fix it because globals can be defined indirectly and lots of scripts might break. In any case, $GLOBALS[] should continue to be the recommended way.
Andi
At 11:24 PM 10/30/2002 +0100, Marcus Boerger wrote:
Attached you see it as a .phpt file (but where is the difference between
your ZE1/2 results).
Part 2: bar_static() is the test that failed in bug20175 but
i think the way Zend Engine 1.1/2 do it now is correct because
they return copies.
Part 3: indeed seems to be an error. Because the manual says one
can return a reference but it shows you can't.
=tests/lang/bug20175.phpt=====
--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
=EOF==============================
At 21:51 30.10.2002, Moriyoshi Koizumi wrote:
I've simplified the test code, and I still have got a strange result with ZE2.------------------------------------------------------ <?php function foo() { static $value; if (!isset($value)) { $value = rand(0,100); } return $value; } function bar() { static $value; if (!isset($value)) { $value = &rand(0,100); } return $value; } print zend_version()."\n"; print foo()."\n"; print foo()."\n"; print bar()."\n"; print bar()."\n"; ?> ------------------------------------------------------ with ZE1 ------------------------------------------------------ 1.3.0 26 26 75 95 ------------------------------------------------------ with ZE2 ------------------------------------------------------ 2.0.0-alpha3 34 34 29 92 ------------------------------------------------------ Is this a known behaviour? Moriyoshi -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php