#39713 [NEW]: Static variables defined in static methods duplicated in derived classes
| From: | paul at digitalbacon dot us | Date: | Sat, 02 Dec 2006 04:24:04 +0000 |
| Subject: | #39713 [NEW]: Static variables defined in static methods duplicated in derived classes | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-105786@lists.php.net to get a copy of this message | ||
From: paul at digitalbacon dot us
Operating system: Linux, Mac OS X 10.4, Windows XP
PHP version: 5.2.0
PHP Bug Type: Scripting Engine problem
Bug description: Static variables defined in static methods duplicated in derived classes
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 bug report at http://bugs.php.net/?id=39713&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=39713&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=39713&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=39713&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=39713&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=39713&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=39713&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=39713&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=39713&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=39713&r=support
Expected behavior: http://bugs.php.net/fix.php?id=39713&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=39713&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=39713&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=39713&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=39713&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=39713&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=39713&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=39713&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=39713&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=39713&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=39713&r=mysqlcfg