From: don at smugmug dot com Operating system: Ubuntu 12.04 PHP version: 5.4.10 Package: Class/Object related Bug Type: Bug Bug description:Parent class incorrectly using child constant in class property
Description: ------------ Class properties that rely on potentially inherited class constants have unpredictable behavior. Since PHP doesn't support Child class properties referencing static values like static::CONST, the meaning of self::CONST is ambiguous. One of two things should happen: 1. It should use the value defined in the actual class in question (like self:: is used throughout the rest of PHP). 2. It should treat self:: in this case, since it's compile-time and not late static binding, like static:: and walk the inheritance tree, delivering the result for the Child class. Option #1 seems the most sane, but PHP often behaves like it intends #2 to work. But not always... In the provided examples, 'brokenA.php' behaves like #1, above, while 'brokenB.php' and 'brokenC.php' behave like #2. The only thing that's changed is the order in which the classes are require()'d. In a complex script, with autoloaders, class instantiation order isn't predictable, of course, resulting in unpredictable results. Test script: --------------- Example code: https://github.com/onethumb/php-parent-child-constant-bug Expected result: ---------------- Consistent results for Baz->table. Either 'foo' or 'baz' 100% of the time, rather than mixed up depending on require() order. Have a preference for adding static::CONST to PHP and making self::CONST behave like self:: does in the rest of the language (resulting in Baz->table == 'baz' in the examples if we used static::CONST), but if that's not preferable for some reason, self::CONST should probably behave like self:: everywhere else (resulting in Baz->table == 'foo' in the examples). Actual result: -------------- brokenA.php: Bar Object ( [table] => bar ) Baz Object ( [table] => foo ) brokenB.php: Bar Object ( [table] => bar ) Baz Object ( [table] => baz ) brokenC.php: Baz Object ( [table] => baz ) Bar Object ( [table] => bar ) Baz Object ( [table] => baz ) -- Edit bug report at https://bugs.php.net/bug.php?id=63976&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=63976&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=63976&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=63976&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=63976&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=63976&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=63976&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=63976&r=needscript Try newer version: https://bugs.php.net/fix.php?id=63976&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=63976&r=support Expected behavior: https://bugs.php.net/fix.php?id=63976&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=63976&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=63976&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=63976&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63976&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=63976&r=dst IIS Stability: https://bugs.php.net/fix.php?id=63976&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=63976&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=63976&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=63976&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=63976&r=mysqlcfg