Skip to content

Callable not considered contravariant with Closure #19178

@Crell

Description

@Crell

Description

The following code:

<?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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions