-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Open
Description
Description:
Currently, when binding a new object to Closure
, which is an object of a different class, a "warning" is logged in the log, but the method itself has no effect, which can be confusing.
It seems to me better to add Throwable
in such a scenario, it will make it easier to handle the case.
The following code:
<?php
class SomeClass
{
public function testMethod() {
echo 123;
}
}
class OtherClass
{
public function testMethod() {
echo 234;
}
}
$a = new SomeClass();
$b = new OtherClass();
$closure = Closure::fromCallable([$a, 'testMethod']);
try {
$closure->bindTo($b);
} catch (Throwable $e) {
echo 'Oh, sorry u cannot do it!';
return;
}
$closure();
Resulted:
Warning: Cannot bind method SomeClass::testMethod() to object of class OtherClass in /in/VgKd4 on line 23
123
Expected:
Oh, sorry u cannot do it!
Workaround:
As it stands, to be able to handle such a scenario you need to use Reflection as follows:
$reflection = new ReflectionFunction($closure);
$oldThis = $reflection->getClosureThis();
if ($oldThis && $oldThis::class === $b::class) {
$closure->bindTo($b);
} else {
echo 'Oh, sorry u cannot do it!';
}
Of course, this code can be simplified, but a simple try/catch is a much better solution.
Especially since I assume that the parser will check again if the object is of the same class when executes bindTo()
PHP Version
PHP 8.2.6
Operating System
Linux