Edit report at http://bugs.php.net/bug.php?id=54202&edit=1
ID: 54202 User updated by: landeholm at gmail dot com Reported by: landeholm at gmail dot com Summary: Closures have invalid context 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: cataphr...@php.net: Excellent, thanks. Previous Comments: ------------------------------------------------------------------------ [2011-03-09 16:38:49] cataphr...@php.net 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. ------------------------------------------------------------------------ [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