|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-05-19 09:27 UTC] 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'; PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 05:00:01 2025 UTC |
It most certainly isn't. Declaring a variable as private static just hides the variable from outside the class. It also allows subclasses to redeclare it. But it doesn't change a thing, and that's not the intended behaviour. An example: class ClassA { private static $cn; public static function setName( $cn ) { self::$cn = $cn; } public static function getName( ) { return self::$cn; } } class ClassB extends ClassA { private static $cn; // with or without this, result is the same } ClassA::setName( 'AAA' ); ClassB::setName( 'BBB' ); print( ClassA::getName() . "\n" ); // prints 'BBB' print( ClassB::getName() . "\n" ); // prints 'BBB' ClassB::setName() is using ClassA as self, when that's not the intended nor propper behaviour. If you call a method on ClassB, it has to affect ClassB, not ClassA - that's how $this works on instances, that's how it should work on static classes. Secondly,if you extend a class, this class call the static variable, it is ok, but if you don't need to extend, you use to declare too in the new class. Example: <?php class ClassA { static $count; static $somevar; } class ClassB extends ClassA { static $somevar; } // another simpler example ClassA::$somevar = 'A'; ClassB::$somevar = 'B'; var_dump(ClassA::$somevar,ClassB::$somevar); ClassA::$somevar = 'A'; var_dump(ClassA::$somevar,ClassB::$somevar);?>