|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74558 Can't rebind closure which returns by Closure::fromCallable()
Submitted: 2017-05-08 20:45 UTC Modified: 2020-11-12 12:49 UTC
From: igorchernin at yahoo dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.1.4 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
19 - 7 = ?
Subscribe to this entry?

 [2017-05-08 20:45 UTC] igorchernin at yahoo dot com
From manual page:
Get error:  Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()

I am getting the error after converting function to Closure and after rebind the Closure to some object.
I can't bind (and also bindTo) closure which returned by Closure::fromCallable(). 

Test script:
class A {
    private $name;
    public function __construct($name)
        $this->name = $name;

// test callable
function getName()
    return $this->name;
$bob = new A("Bob");

$cl1 = Closure::fromCallable("getName");
$cl1 = $cl1->bindTo($bob, 'A');
$result = $cl1();
echo $result

Expected result:

Actual result:
Warning: Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure() on line 18


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-08 20:52 UTC]
The error message should be changed to not mention ReflectionFunctionAbstract::getClosure(), but otherwise the behavior is intentional. Freestanding functions don't have an object scope, so you cannot bind one.
 [2017-05-08 21:35 UTC] igorchernin at yahoo dot com
Thank you, for the clarification. So as I undestood. 
If I use the Closure
$cl2 = function () {
    return $this->name;
It has an object scope (because $cl2 is an object of Closure).
But Closure::fromCallable() creates a Closure from current scope which is not an object scope in case of freestanding function. And $this inside freestanding function is not valuable. Maybe it makes sense to add the information to manual or restrict to use $this inside freestanding functions. Thank you.
 [2020-11-12 12:49 UTC]
-Package: *General Issues +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2020-11-12 12:49 UTC]
The following pull request has been associated:

Patch Name: Fix #74558: Can't rebind closure returned by Closure::fromCallable()
On GitHub:
 [2020-11-16 13:35 UTC]
Automatic comment on behalf of
Log: Fix #74558: Can't rebind closure returned by Closure::fromCallable()
 [2020-11-16 13:35 UTC]
-Status: Assigned +Status: Closed
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Aug 07 22:05:44 2022 UTC