Bug #69586 [NEW]: Resolution of static function variables not using forwarded calling information
| From: | php at lvl dot fastmail dot com | Date: | Wed, 06 May 2015 15:05:20 +0000 |
| Subject: | Bug #69586 [NEW]: Resolution of static function variables not using forwarded calling information | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-192518@lists.php.net to get a copy of this message | ||
From: php at lvl dot fastmail dot com
Operating system:
PHP version: 5.6.8
Package: Unknown/Other Function
Bug Type: Bug
Bug description:Resolution of static function variables not using forwarded calling information
Description:
------------
I ran into some very strange behavior while experimenting with static
function variables. Static variables in the function scope appear to be
resolved using late static binding: the content of the variable in that
function is shared amongst all instances of a class, though each
subclass gets its own separate variable space.
This is pretty logical and works fine. As an example, let's create
classes Base => Animal => {Cat,Dog}. Base has a function that describes
the class, and caches the result in a static function variable
$description. Calling describe() on the three different classes
correctly gives us three different results: http://3v4l.org/Qldve
However, once you override the describe function in Animal and call
parent::describe() from within, it looks like all calling information is
lost: the resolution of $description in Base's function scope now always
points to Animal's variable space. For example: http://3v4l.org/CcrL8
I would have expected the static function variable and
get_called_class() to always refer to the same class/variable space,
parent:: or not.
Overriding the function in one of the childmost classes like Dog gives
an even stranger result: $description is now correctly a separate value
in Cat vs Dog, but calling Animal::describe() now uses the value left by
its child class Cat?! See: http://3v4l.org/61P72
Test script:
---------------
class Base {
public static function describe() {
echo "--> " . ($calculated = "I am a " . get_called_class() .
"\n");
static $cached = null;
if ($cached === null) {
$cached = $calculated;
}
return $cached;
}
}
class Animal extends Base {
public static function describe() {
return 'Hello! ' . parent::describe();
}
}
class Cat extends Animal { }
class Dog extends Animal { }
echo Cat::describe();
echo Dog::describe();
echo Animal::describe();
Expected result:
----------------
--> I am a Cat
Hello! I am a Cat
--> I am a Dog
Hello! I am a Dog
--> I am a Animal
Hello! I am a Animal
Actual result:
--------------
--> I am a Cat
Hello! I am a Cat
--> I am a Dog
Hello! I am a Cat
--> I am a Animal
Hello! I am a Cat
--
Edit bug report at https://bugs.php.net/bug.php?id=69586&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69586&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69586&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69586&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69586&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69586&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69586&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69586&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69586&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69586&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69586&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69586&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69586&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69586&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69586&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69586&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69586&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69586&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69586&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69586&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69586&r=mysqlcfg