#39713 [Bgs]: Static variables defined in static methods duplicated in derived classes
| From: | paul at digitalbacon dot us | Date: | Sun, 03 Dec 2006 00:47:38 +0000 |
| Subject: | #39713 [Bgs]: Static variables defined in static methods duplicated in derived classes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-105807@lists.php.net to get a copy of this message | ||
ID: 39713
User updated by: paul at digitalbacon dot us
Reported By: paul at digitalbacon dot us
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux, Mac OS X 10.4, Windows XP
PHP Version: 5.2.0
New Comment:
From the manual:
...A static variable exists only in a local function scope,
but it does not lose its value when program execution leaves
this scope.
I don't see why the use of the scope resolution operator
(::) should change a static method's local function scope.
Can a single static method really have multiple local scopes
if using inheritance?
Previous Comments:
------------------------------------------------------------------------
[2006-12-02 11:49:09] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
------------------------------------------------------------------------
[2006-12-02 04:24:04] paul at digitalbacon dot us
Description:
------------
When using a static variable defined inside a parent class's
static method block, different outcomes can occur depending on
the scope name used when calling.
The effect is that a non-overridden method in the child class
is allocated a completely separate static variable... not how
it works in C or C++, which I know PHP is not.
I encountered this problem implementing a singleton pattern in
a parent class. I apologize for the reproduction code, but it
does illustrate my perceived problem.
The closest bugs I found, relate to static class attributes,
not regular static variables.
Reproduce code:
---------------
<?php
class TheParent {
public static function staticMethod()
{
static $theStaticValue = true;
if ($theStaticValue == true) {
$theStaticValue = false;
return true;
} else return $theStaticValue; // false
}
}
class TheChild extends TheParent {}
$a1 = TheParent::staticMethod();
$a2 = TheParent::staticMethod();
$b1 = TheChild::staticMethod();
$b2 = TheChild::staticMethod();
echo '$a1: ', $a1, "\n"; // true
echo '$a2: ', $a2, "\n"; // false
echo '$b1: ', $b1, "\n"; // true (I believe this should be false)
echo '$b2: ', $b2, "\n"; // false
?>
Expected result:
----------------
My intuition tells me that the parent's static variable should
be used regardless of the scope name used... unless it's
actually redefined in the child class.
In this example, I expect the output to be:
$a1: 1
$a2:
$b1:
$b2:
Is local static method data not shared?
Actual result:
--------------
$a1: 1
$a2:
$b1: 1
$b2:
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=39713&edit=1