#28442 [Opn]: Changing a static variables in a class changes it across sub/super classes.

From: Date: Fri, 21 May 2004 03:28:14 +0000
Subject: #28442 [Opn]: 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-59826@lists.php.net to get a copy of this message
ID: 28442 User updated by: kell_pt at users dot sf dot net Reported By: kell_pt at users dot sf dot net Status: Open Bug Type: Zend Engine 2 problem Operating System: any PHP Version: 5CVS-2004-05-19 (dev) New Comment: I would also like to call your attention to Bug #26930 which was dismissed as "Bogus", where in fact it's a very valid problem. Seems like the behaviour for static data/methods doesn't follow the OO paradigm very close. I hope you guys don't dismiss these problems and provide a sound implementation. :) Cheers. Previous Comments: ------------------------------------------------------------------------ [2004-05-19 09:27:20] kell_pt at users dot sf dot net Description: ------------ 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: --------------- 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=28442&edit=1

« previous php.bugs (#59826) next »