Skip to content

Awkward error using first-class callable syntax with missing function #8623

Open

Description

@othercorey

Description

The following code:

<?php

'missing'(...);

Resulted in this output:

Fatal error: Uncaught Error: Call to undefined function missing() 

But I expected this output instead:

Fatal error: Uncaught Error: Unable to create closure from undefined function missing() 

When using the first-class callable syntax on a missing function, the error message claims the user is calling a function. However, this should only be creating a closure.

I thought the (...) syntax would only try to create a closure. Does it actually try to call the function with a ... argument if it's not found?

If you use the original Closure::fromCallable() method, the error is different.

Closure::fromCallable('missing');
Uncaught TypeError: Failed to create closure from callable: function "missing" not found or invalid function name

PHP Version

8.1

Operating System

No response

Activity

  1. cmb69 commented on May 24, 2022

    @cmb69
    Member

    The problem is that the callable syntax causes an INIT_* opcode to be emitted, and afterwards an CALLABLE_CONVERT to convert to the call frame to closure. However, if the function is not defined, the INIT_* fails, causing the error message.

    Having a proper error message has been discussed, but is not generally possible. Maybe we should just document this issue more clearly?

  2. othercorey commented on May 24, 2022

    @othercorey
    ContributorAuthor

    Maybe we should just document this issue more clearly?

    Documentation of the expected error would greatly improve this.

  3. cmb69 commented on May 24, 2022

    @cmb69
    Member

    I keep this ticket here for a while; maybe someone comes up with a way to improve the error message.

  4. nikic commented on May 24, 2022

    @nikic
    Member

    Any thoughts on the suggestion to use more generic error messages (that don't use "call" terminology) from #7019 (comment)?

  5. cmb69 commented on May 25, 2022

    @cmb69
    Member

    @nikic, like you already said there: "Though maybe that makes the message in the common case slightly harder to understand." Particularly, the word "reference" might cause some confusion regarding references (&). OTOH, the wording is actually more precise even for the call-case.

  6. othercorey commented on May 25, 2022

    @othercorey
    ContributorAuthor

    Particularly, the word "reference" might cause some confusion regarding references (&). OTOH, the wording is actually more precise even for the call-case.

    If I may offer an opinion, even though it's throwing an error, prefixing with "Unable to", might help.

    From:

    Reference to private method Foo::bar() from Baz

    To:

    Unable to reference to private method Foo::bar() from Baz

    If the code that's throwing the error is always just resolving functions, then you could say Locating instead of Reference. If locating the function fails then naturally any use of it would fail after.

  7. othercorey commented on Jul 6, 2022

    @othercorey
    ContributorAuthor

    @nikic I don't suppose there's a chance of making this error more generic for 8.2?

  8. GuilhermeAltmann commented on Nov 22, 2026

    @GuilhermeAltmann

    I'd like to pick this up.

    Following the earlier discussion (particularly nikic's comment about not threading call-kind information through the get_method handler), here is a proposal limited to the function code paths:

    First-class callables compile to an INIT_* opcode immediately followed by ZEND_CALLABLE_CONVERT. On the error paths for undefined functions we can check whether the next opcode is ZEND_CALLABLE_CONVERT and emit a closure-specific message instead of the misleading "Call to undefined function". This covers:

    • ZEND_INIT_FCALL_BY_NAME / ZEND_INIT_NS_FCALL_BY_NAME (via zend_undefined_function_helper),
    • ZEND_INIT_DYNAMIC_CALL for string callables ('missing'(...), $name(...)), and
    • constant-expression FCC evaluation in zend_ast.c, where the AST is already known to be a first-class callable.

    Suggested wording, aligned with Closure::fromCallable()'s existing TypeError ("Failed to create closure from callable: ..."):

    Failed to create closure from undefined function missing()

    Common calls are unaffected (missing() still reports "Call to undefined function missing()").

    Out of scope: methods (per #7019, the error comes from the get_method object handler) and partial applications (foo(?) — ZEND_CALLABLE_CONVERT_PARTIAL is not adjacent to the INIT opcode, so runtime PFAs keep the current message).

    I have a working patch with tests (full Zend suite green on NTS debug, plus ZTS and opcache sanity runs). If the approach and wording look good, I'll open a PR.

    (The quoted paragraph above was drafted with the help of an LLM coding agent.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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