php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28442 Changing a static variables in a class changes it across sub/super classes.
Submitted: 2004-05-19 09:27 UTC Modified: 2005-03-10 14:25 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:2 (50.0%)
From: kell_pt at users dot sf dot net Assigned: helly (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.3 OS: *
Private report: No CVE-ID: None
 [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';



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-21 05:28 UTC] kell_pt at users dot sf dot net
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.
 [2004-05-21 18:12 UTC] helly@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

What you want is \'private static\'
 [2004-05-22 04:13 UTC] kell_pt at users dot sf dot net
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,
 [2004-05-22 04:17 UTC] kell_pt at users dot sf dot net
Secondly, there have been more bug reports regarding this issue, or different effects of the model. Anyone having programmed in other enviroments where class inheritance is possible will not feel at home with this, and also it doesn't add anything to the language - it is not only a limitation, but also misleading behaviour. We'd like to see a bit more discussion on this, it *really* isn't a bogus problem. :)

Cheers, and I apologize for the insistence, but we're using PHP5 for its new OO features, and it's being hard to cope with the fact that this seems to be overlooked. Hopefully it will be fixed (can't see a reason why it wouldn't).

Cheers.
 [2005-03-10 14:17 UTC] helly@php.net
Your code assumption is not fully correct. A static variable will be inherited unless not defined again. However this 'unless' part is wrong, it rsults in a compiler error.
 [2005-03-10 14:25 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

The static constructor in the example above would be an interesting feature request, feel free to open it as a new bug.
 [2014-03-08 18:21 UTC] duranguenze at gmail dot com
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);?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC