Req #51720 [Opn->Bgs]: static properties in subclasses should take on own value w/o redeclaration
Edit report at http://bugs.php.net/bug.php?id=51720&edit=1
ID: 51720
Updated by: johannes@php.net
Reported by: dtomasiewicz at gmail dot com
Summary: static properties in subclasses should take on own
value w/o redeclaration
-Status: Open
+Status: Bogus
Type: Feature/Change Request
Package: Class/Object related
Operating System: all
PHP Version: 5.3.2
New Comment:
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
The protected static variable is inherited by the children. Make it
private if it should belong to a specific class context.
Previous Comments:
------------------------------------------------------------------------
[2010-05-02 00:01:20] dtomasiewicz at gmail dot com
Description:
------------
Something I've found quite annoying about the Late Static Binding
implementation
is the need to re-declare static variables in subclasses to allow them
to have
their own values. (see Test Script)
In order to get the desired results, you need to re-declare the static
variable
in the subclasses, which of course goes against every fundamental
principle of
DRY if you're going to have a large number of subclasses and static
properties.
I can't find a work-around to this other than to re-declare the static
variables, which sort of puts us in the same boat we were in before 5.3
when we
would need to redeclare static methods in subclasses too.
I'm not sure why this is behaving this way-- if you don't want
subclasses to
have their own value, you should make it private or use the self::
keyword when
referencing the variable in the superclass.
There is a note on the PHP Late Static Binding page that reads "static::
does
not work like $this for static methods! $this-> follows the rules of
inheritance
while static:: doesn't. This difference is detailed later on this manual
page."
but the difference is NOT detailed later on that page, and I can't find
any
details whatsoever.
Test script:
---------------
<?php
class Foo {
protected static $whoAmI;
public static function init() {
static::$whoAmI = get_called_class();
}
public static function who() {
echo static::$whoAmI."\n";
}
}
class BarOne extends Foo {}
class BarTwo extends Foo {}
BarOne::init(); // sets the whoAmI property in Foo to BarOne
BarTwo::init(); // sets the whoAmI property in Foo to BarTwo
BarOne::who(); // outputs "BarTwo"
BarTwo::who(); // also outputs "BarTwo"
Expected result:
----------------
BarOne
BarTwo
Actual result:
--------------
BarTwo
BarTwo
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51720&edit=1
Thread (2 messages)