From: Operating system: all PHP version: 5.3.2 Package: Class/Object related Bug Type: Feature/Change Request Bug description:static properties in subclasses should take on own value w/o redeclaration
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 bug report at http://bugs.php.net/bug.php?id=51720&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51720&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51720&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51720&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51720&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51720&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51720&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51720&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51720&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51720&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51720&r=support Expected behavior: http://bugs.php.net/fix.php?id=51720&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51720&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51720&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51720&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51720&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51720&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51720&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51720&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51720&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51720&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51720&r=mysqlcfg