|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 23:00:01 2025 UTC |
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.