#47620 [Opn->Bgs]: Weird static keyword behaviour
| From: | dmitry@php.net | Date: | Wed, 18 Mar 2009 15:02:49 +0000 |
| Subject: | #47620 [Opn->Bgs]: Weird static keyword behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-134869@lists.php.net to get a copy of this message | ||
ID: 47620
Updated by: dmitry@php.net
Reported By: olemarkus dot with at student-media dot no
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: *
PHP Version: 5.3CVS-2009-03-11 (snap)
-Assigned To:
+Assigned To: dmitry
New Comment:
It's hard to explain, but it's expected.
In case the method getName() doesn't declared as "static" it's called
dynamically with current $this (A). This feature is kept for ages for
backward compatibility. You can even see "strict" warning:
Strict Standards: Non-static method B::getName() should not be called
statically, assuming $this from incompatible context in ...
In case you declare getName() "static" you'll get that you expect.
Previous Comments:
------------------------------------------------------------------------
[2009-03-11 14:05:59] jani@php.net
And here is the code:
<?php
namespace A {
class A {
function __construct() {
echo \C\C::getName() . "\n";
}
}
}
namespace B {
abstract class B {
static protected $name = '';
function getName() {
return static::$name;
}
}
}
namespace C {
class C extends \B\B {
static protected $name = 'C';
}
}
namespace {
echo \C\C::getName() . "\n";
$a = new \A\A();
}
?>
------------------------------------------------------------------------
[2009-03-11 07:18:24] olemarkus dot with at student-media dot no
Description:
------------
When using static:: in a parent class to refer to the child class that
was called, the keyword is resolved to the caller class instead.
Reproduce code:
---------------
http://olemarkus.com/issue.txt
Expected result:
----------------
I expect both calls to \C\C::getName() to yield the same result:
C
C
Actual result:
--------------
The second one gives the following output:
C
Fatal error: Access to undeclared static property: A\A::$name in
issue.php on line 13
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47620&edit=1