|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-06-29 08:00 UTC] picht at eventit dot ag
Description:
------------
When calling a non-static function statically the late static binding context
still points to the calling class context. This should be documented on the static
keyword page.
Test script:
---------------
<?php
class a {
public static function foo()
{
echo 'foo a';
}
public function bar()
{
// expecting to call a::foo or b::foo
// calls c::foo instead
static::foo();
}
}
class b extends a {
public static function foo()
{
echo 'foo b';
}
}
class c {
public function test()
{
b::bar();
}
public static function foo()
{
echo 'foo c';
}
}
$c = new c();
$c->test();
Expected result:
----------------
Documentation suggest b::foo will be called
Actual result:
--------------
Documentation warns about c::foo getting called
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 09:00:01 2025 UTC |
This is a simpler example: <?php class Inner { public static function internalStatic() { } public function NonStaticCalledStaticLate() { static::internalStatic(); } public function NonStaticCalledStaticSelf() { self::internalStatic(); } } class Outer { public function testWorking() { Inner::NonStaticCalledStaticSelf(); } public function testBroken() { Inner::NonStaticCalledStaticLate(); } } // Working Inner::NonStaticCalledStaticLate(); // Working Inner::NonStaticCalledStaticSelf(); $obj = new Outer(); // Working $obj->testWorking(); // Broken $obj->testBroken(); // PHP Fatal error: Call to undefined method Outer::internalStatic() in test.php on line 9 echo "Worked"; ?>