[php-src] Issue #19178: Callable not considered contravariant with Closure
| From: | Crell | Date: | Fri, 18 Jul 2025 18:29:10 +0000 |
| Subject: | [php-src] Issue #19178: Callable not considered contravariant with Closure | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250455@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/19178
Author: Crell
### Description
The following code:
```php
<?php
class Test {
public private(set) \Closure $action {
set (callable $value) {
if (!$value instanceof \Closure) {
$value = $value(...);
}
$this->action = $value;
}
}
}
```
Resulted in this output:
```
Fatal error: Type of parameter $value of hook Test::$action::set must be compatible with property
type
```
But I expected this output instead:
```
I expected no error.
```
As far as I am aware,
callable includes \Closure. You can pass a
Closure to a callable. So I would expect this to work. However, it gets
rejected. Specifying set(\Closure|callable) { ... } is a workaround.
It seems like either the variance detection is buggy or the definition of callable is wonkier than I
thought.
### PHP Version
```plain
PHP 8.4.8 (cli) (built: Jun 9 2025 13:49:53) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.8, Copyright (c) Zend Technologies
with Zend OPcache v8.4.8, Copyright (c), by Zend Technologies
with Xdebug v3.4.0, Copyright (c) 2002-2024, by Derick Rethans
(Though 3v4l confirms this error on all 8.4 releases.)
```
### Operating System
_No response_