php / php/php-src

in return type class with __invoke method is not a callable

Open
#18,161 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Engine Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
interface I {
    public function test(): callable;
}
class Invokable {
    public function __invoke() {}
}
var_dump(is_callable(new Invokable()));
class C implements I {
    public function test(): Invokable {}
}

Resulted in this output:

bool(true)

Fatal error: Declaration of C::test(): Invokable must be compatible with I::test(): callable in /in/PfBWX on line 10

Process exited with code 255.

Methods should be able to restrict callable return types from interfaces and parent classes to more specific types that unconditionally fall within the callable type. Any class or interface that contains an __invoke method should be considered a callable type an be allowed as a more specific subtype of callable in subclasses. Also \Closure should be allowed as well.

PHP Version

8.4.5

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the PHP reproducer from the issue and inspect how return-type compatibility handles callable and class types. The issue is done when classes with __invoke and Closure can be accepted as narrower return types for callable declarations, with compatibility tests covering the reported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.