php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54202 Closures have invalid context
Submitted: 2011-03-09 15:44 UTC Modified: 2011-03-09 17:03 UTC
From: landeholm at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.5 OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: landeholm at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-09 15:44 UTC] 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..

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-09 16:38 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2011-03-09 16:38 UTC] cataphract@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 17:03 UTC] landeholm at gmail dot com
cataphract@php.net: Excellent, thanks.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Aug 01 11:00:02 2025 UTC