Bug #28442 [Com]: Changing a static variables in a class changes it across sub/super classes.
| From: | duranguenze at gmail dot com | Date: | Sat, 08 Mar 2014 18:21:30 +0000 |
| Subject: | Bug #28442 [Com]: 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-184623@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=28442&edit=1
ID: 28442
Comment by: duranguenze at gmail dot com
Reported by: kell_pt at users dot sf dot net
Summary: Changing a static variables in a class changes it
across sub/super classes.
Status: Closed
Type: Bug
Package: Scripting Engine problem
Operating System: *
PHP Version: 5.0.3
Assigned To: helly
Block user comment: N
Private report: N
New Comment:
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);?>
Previous Comments:
------------------------------------------------------------------------
[2005-03-10 14:25:48] 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.
------------------------------------------------------------------------
[2005-03-10 14:17:49] 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.
------------------------------------------------------------------------
[2004-05-22 04:17:45] 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.
------------------------------------------------------------------------
[2004-05-22 04:13:56] 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-21 18:12:34] 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\'
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=28442
--
Edit this bug report at https://bugs.php.net/bug.php?id=28442&edit=1