|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-02-01 02:29 UTC] drewparoski at gmail dot com
Description:
------------
When a PHP closure is created, it is supposed to capture the late bound class of the enclosing function. There are a number of cases involving derived classes and static methods or static closures (or both) where it doesn't work correctly.
The test script I've included demonstrates the problem. I've tested it on PHP 5.6.0alpha1, but I can also reproduce the problem on various builds of PHP 5.5.
Test script:
---------------
<?php
class A {
static function name() { return 'A'; }
function foo() {
$fn = function() { return static::name(); };
echo static::name() . ' vs ' . $fn() . "\n";
}
function bar() {
$fn = static function() { return static::name(); };
echo static::name() . ' vs ' . $fn() . "\n";
}
static function baz() {
$fn = function() { return static::name(); };
echo static::name() . ' vs ' . $fn() . "\n";
}
}
class B extends A {
static function name() { return 'B'; }
}
function test() {
(new B)->foo();
B::foo();
(new B)->baz();
B::baz();
(new B)->bar();
B::bar();
}
test();
Expected result:
----------------
B vs B
B vs B
B vs B
B vs B
B vs B
B vs B
Actual result:
--------------
B vs B
B vs A
B vs A
B vs A
B vs A
B vs A
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 22:00:01 2025 UTC |
The fix for this issue introduces a change in previous behaviour for private static methods being called from a closure in a public / protected static method. The following script used to output "barbar", currently a fatal error is triggered: Test script: -------------- <?php class Foo { private static function bar() { echo 'bar'; } public static function baz() { $f = function () { self::bar(); }; $f(); } } class Bar extends Foo {} Foo::baz(); Bar::baz();