-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal error: Call to undefined method invokeArgs() after closure rebinding #3772
Comments
It looks like it's a result of the way that |
Yes, you are right, |
I know how to fix this from PHP side, need to rewrite last part of this method (I can send PR for that): public function getClosure($object = null)
{
// < original top part of code >
$methodName = $this->name;
$closure = function (...$args) use ($methodName) {
$isDynamic = isset($this);
$result = $isDynamic ? $this->$methodName(...$args) : static::$methodName(...$args);
return $result;
};
return $closure->bindTo($object, $this->class); With this patch closures from methods can work with rebinding, but scope handling is still wrong for static and dynamic closures: class A {
public static function test() {
echo 'First', ',', get_called_class();
}
}
class B extends A {
public static function test() {
echo 'Second';
}
}
$refClosure = (new ReflectionMethod(A::class, 'test'))
->getClosure(null)
->bindTo(null, B::class);
$refClosure();
// Expected output:
First, B
// Real output is from B::test() invocation instead of A::test() with scope=B:
Second For my first example (in the issue body) I got following:
Looks like this issue has common roots with #2486 |
The reason that isn't working is because class Foo {
function test() { echo "In foo\n"; }
public function getBaz() {
return function () { static::test(); };
}
}
class Bar {
function test() { echo "In bar\n"; }
}
$c = (new Foo)->getBaz();
$c();
$d = $c->bindTo(null, Bar::class);
$d(); I get the following, illustrating that
Running the following example will give the same result in both HHVM and PHP5, showing that static scope binding is working consistently. class Foo {
public function getBaz() {
return function () { echo get_called_class(); };
}
}
class Bar {}
$c = (new Foo)->getBaz();
$c();
echo "\n";
$d = $c->bindTo(null, Bar::class);
$d();
echo "\n"; PHP 5 and HHVM both correctly output
|
W.r.t. submitting this as a PR, unfortunately The other issue (#2486) is something that actually came up while working on this. We don't support invoking instance methods from unrelated classes on arbitrary objects. Limited support could probably be added using logic from |
Thank you, @paulbiss for explanation. Seems I should change
I know about limitations of Repo mode, so it's not an issue for me, it can be a requirement for my framework. However, I hope that there will be a partial support in HHVM for my issues in any mode :) |
Is there a documentation or recommendation about ini keys for hhvm? I will put a check into the |
Unfortunately, $className = $this->getDeclaringClass()->name;
$methodName = $this->name;
$closure = function (...$args) use ($methodName, $className) {
$result = forward_static_call_array(array($className, $methodName), $args);
return $result;
};
return $closure->bindTo($object, $this->class); Just checked this: it is working under HHVM for static and dynamic methods and equals to the standard PHP behaviour. |
The runtime option I added for this is |
Marking this as wishlist, as altering the scope of arbitrary member functions is likely to break things inside the JIT. |
It's pity ... From PHP side I couldn't create a reliable code that will work for all possible cases (some of them still failing) |
Ping |
I don't think anyone is working on this, it's marked as wishlist. |
@paulbiss ok, understood, hope it will be implemented in the next year :) |
Just ran into this issue myself. Is there a workaround people have been using? |
I'm going to close this issue because of dropping support for the HHVM. Anyway, thanks guys! It was a nice try! And almost everything worked except of several issues. |
This is still an issue I would like to see fixed. Does it hurt to leave it open until resolved? |
I have a strange issue with closure binding:
Expected output is 'First' if everything is OK.
The text was updated successfully, but these errors were encountered: