php.net |  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 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: igorchernin at yahoo dot com
New email:
PHP Version: OS:

 

 [2017-05-08 20:45 UTC] igorchernin at yahoo dot com
Description:
------------
---
From manual page: http://www.php.net/closure.fromcallable
---
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:
---------------
<?php
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:
----------------
Bob

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

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-08 20:52 UTC] nikic@php.net
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] cmb@php.net
-Package: *General Issues +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2020-11-12 12:49 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #74558: Can't rebind closure returned by Closure::fromCallable()
On GitHub:  https://github.com/php/php-src/pull/6424
Patch:      https://github.com/php/php-src/pull/6424.patch
 [2020-11-16 13:35 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=78773890f6b0d82b29e0b869f3f3b22174cea217
Log: Fix #74558: Can't rebind closure returned by Closure::fromCallable()
 [2020-11-16 13:35 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC