Bug #66622 [Opn]: Closures do not correctly capture the late bound class (static::) in some cases

From: Date: Tue, 04 Feb 2014 01:39:45 +0000
Subject: Bug #66622 [Opn]: 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-184147@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
 User updated by:    drewparoski at gmail dot com
 Reported by:        drewparoski at gmail dot com
 Summary:            Closures do not correctly capture the late bound
                     class (static::) in some cases
 Status:             Open
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   CentOS Linux 6.3
-PHP Version:        5.6.0alpha1
+PHP Version:        5.5.8
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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


Thread (7 messages)

« previous php.bugs (#184147) next »