#39044 [Opn->Bgs]: Changing a static variables in a class changes it across sub/super classes.

From: Date: Thu, 05 Oct 2006 11:21:27 +0000
Subject: #39044 [Opn->Bgs]: Changing a static variables in a class changes it across sub/super classes.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-103126@lists.php.net to get a copy of this message
ID: 39044 Updated by: tony2001@php.net Reported By: matti at nitro dot fi -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: * PHP Version: 5.1.6 New Comment: This is the way C++ and Java work. When a static member is inherited, it's the same value across all classes. But if you redeclare it in the child class, changing it wouldn't affect parent. Previous Comments: ------------------------------------------------------------------------ [2006-10-05 07:47:41] matti at nitro dot fi Description: ------------ Bug is still open in release versions. When it will be fixed? http://bugs.php.net/bug.php?id=28442 quote------------ [19 May 2004 9:27am UTC] kell_pt at users dot sf dot net It isn't possible to override static variable values in classes, seeing as the same variable is shared across the whole hierarchy of classes. Each child class should be able to have their own value for a static variable. A good example is trying to have a counter of the number of instances per class. ClassB::$count will always be the same as ClassA::$count. It is possible that you don't consider this a bug, but it is quite against the OOP paradigm, and worth a note. Basically, when loading a subclass, the default values for the variables should be loaded, and a new variable (memory space) created, instead of keeping a reference to the superclass' static variable. This is somehow related to Bug #16245 (which regards static variables declared within functions). But where the behaviour in such a situation is a bit unspecified, in this case it's quite against OO programming. Reproduce code: --------------- quote------------ [19 May 2004 9:27am UTC] kell_pt at users dot sf dot net class ClassA { static $count; static $somevar; static __construct() { self::$count++; // this won't work as expected } } class ClassB extends ClassA { } // another simpler example ClassA::$somevar = 'A'; ClassB::$somevar = 'B'; // ClassA::$somevar is now 'B' instead of 'A'; ClassA::$somevar = 'A'; // ClassB::$somevar is now 'A' instead of 'B'; ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39044&edit=1

« previous php.bugs (#103126) next »