[php-src] Issue #11271: Closure::bindTo Throwable instead of warning when passing object other class
| From: | LordFireen | Date: | Thu, 18 May 2023 23:16:03 +0000 |
| Subject: | [php-src] Issue #11271: Closure::bindTo Throwable instead of warning when passing object other class | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244485@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11271
Author: LordFireen
### 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
<?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!
```
As it stands, to be able to handle such a scenario you need to use Reflection as follows:
```php
$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