Bug #54202 [Opn->Bgs]: Closures have invalid context
| From: | cataphract@php.net | Date: | Wed, 09 Mar 2011 15:38:50 +0000 |
| Subject: | Bug #54202 [Opn->Bgs]: Closures have invalid context | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-159342@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54202&edit=1
ID: 54202
Updated by: cataphract@php.net
Reported by: landeholm at gmail dot com
Summary: Closures have invalid context
-Status: Open
+Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: Irrelevant
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
See http://wiki.php.net/rfc/closures/object-extension
; binding the
closure scope to the scope of where it's declared is already implemented
in trunk.
Previous Comments:
------------------------------------------------------------------------
[2011-03-09 15:44:08] landeholm at gmail dot com
Description:
------------
It doesn't make sense that closures declared inside methods are run with
global context instead of the same context as the method. This is
incredibly annoying as it there are no way to reference
private/protected functions in closures. It's not possible to acquire a
reference to the private/protected functions and pass them to the
closure either so you're locked out from a developers perspective.
See test script.
The fix should be simple. Just inherit the access-context when closures
are declared...
Test script:
---------------
class Foo {
private function bar($a, $b) {
return $a + $b;
}
public function baz($c) {
$biz_fn = function($c) {
return Foo::bar(5, 4) - $c;
};
return $biz_fn($c);
}
}
$foo = new Foo();
echo $foo->baz(3);
Expected result:
----------------
"6"
Actual result:
--------------
Fatal error: Call to private method Foo::bar() from context '' in..
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54202&edit=1