Re: Bug #20175 (Static vars can't store ref to new instance)

From: Date: Wed, 30 Oct 2002 22:24:45 +0000
Subject: Re: Bug #20175 (Static vars can't store ref to new instance)
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-90345@lists.php.net to get a copy of this message
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


« previous php.dev (#90345) next »