Bug #66622 [Com]: Closures do not correctly capture the late bound class (static::) in some cases
| From: | taco at procurios dot nl | Date: | Fri, 05 Dec 2014 11:06:40 +0000 |
| Subject: | Bug #66622 [Com]: Closures do not correctly capture the late bound class (static::) in some cases | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188930@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66622&edit=1
ID: 66622
Comment by: taco at procurios dot nl
Reported by: drewparoski at gmail dot com
Summary: Closures do not correctly capture the late bound
class (static::) in some cases
Status: Closed
Type: Bug
Package: Scripting Engine problem
Operating System: CentOS Linux 6.3
PHP Version: 5.5.8
Assigned To: levim
Block user comment: N
Private report: N
New Comment:
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();
Previous Comments:
------------------------------------------------------------------------
[2014-06-09 02:07:01] stas@php.net
The fix for this bug has been committed.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
------------------------------------------------------------------------
[2014-05-11 03:05:07] levim@php.net
Github is down at the moment; pull request will come in on Monday.
------------------------------------------------------------------------
[2014-05-08 18:06:46] levim@php.net
Related To: Bug #67222
------------------------------------------------------------------------
[2014-02-04 01:39:45] drewparoski at gmail dot com
Verified this happens on PHP 5.5.8, which is the current official release. Updated the version
number for this bug accordingly, though it's worth noting this bug still happens on PHP
5.6.0alpha1 as well.
------------------------------------------------------------------------
[2014-02-01 02:29:31] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66622&edit=1